This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple.min.js
1 lines (1 loc) · 2.97 KB
/
simple.min.js
1
!function(root,factory){if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else if(typeof exports==="object"){module.exports=factory(require("jquery"))}else{root.Simple=factory(root.jQuery)}}(this,function($){var Simple={};var Events=Simple.Events={on:function(event,callback,context){var callbacks=this._callbacks||(this._callbacks={});var events=callbacks[event]||(callbacks[event]=[]);events.push({callback:callback,context:context})},off:function(event,callback,context){if(!callback&&!context){delete this._callbacks[event]}var events=this._callbacks[event]||[];for(var i=0;i<events.length;i++){if(!(callback&&events[i].callback!==callback||context&&events[i].context!==context)){events.splice(i,1)}}},trigger:function(event){var args=Array.prototype.slice.call(arguments,1);var callbacks=this._callbacks||{};var events=callbacks[event]||[];for(var i=0;i<events.length;i++){events[i].callback.apply(events[i].context||this,args)}}};Simple.events=$.extend({},Events);var View=Simple.View=function(options){this.el=options.el;this.delegateEvents();this.initialize(options)};$.extend(View.prototype,Events,{initialize:function(){},render:function(){},$:function(selector){return this.el.find(selector)},DOM:function(){return this.$.apply(this,arguments)},delegateEvents:function(){if(!this.events)return;for(var key in this.events){var methodName=this.events[key],method=$.proxy(this[methodName],this),match=key.match(/^(\w+)(:?\s+(.*))?$/),eventName=match[1],selector=match[2];this.el.on(eventName,selector,method)}}});var Model=Simple.Model=function(options){this.attributes=this.defaults||{};this.attrs(options||{});this.initialize(options)};$.extend(Model.prototype,Events,{initialize:function(){},fetch:function(options){return this._performRequest("fetch",this,options||{},{})},save:function(options){return this._performRequest("save",this,options||{},{type:"POST",data:JSON.stringify(this.attributes),contentType:"application/json"})},_performRequest:function(type,model,options,additionalParams){this.trigger(type+":started");var params={url:typeof model.url==="function"?model.url():model.url,dataType:options.dataType||model.dataType||"json",success:function(data){model.attrs(data);if(typeof options.success!=="undefined"){options.success(data)}else{model.trigger(type+":finished")}},error:function(jqXHR,resp){if(typeof options.error!=="undefined"){options.error()}else{model.trigger(type+":error",resp)}}};return $.ajax($.extend(params,additionalParams))},attr:function(name,value){if(typeof value==="undefined"){return this.attributes[name]}else{this.attributes[name]=value}},attrs:function(attributes){if(typeof attributes==="undefined"){return $.extend({},this.attributes)}else{$.extend(this.attributes,attributes)}}});var ctor=function(){};View.extend=Model.extend=function(properties){var parent=this;var child=function(){parent.apply(this,arguments)};ctor.prototype=parent.prototype;child.prototype=new ctor;$.extend(child.prototype,properties);child.extend=parent.extend;return child};return Simple});