(function(){
window.Youmi = {};
Youmi.VERSION = "友吃1.2";
var Events = Youmi.Events = {
on: function(name, callback, context) {
this._events || (this._events = {});
var events = this._events[name] || (this._events[name] = []);
events.push({callback: callback, context: context, ctx: context || this});
return this;
},
trigger:function(name,opts){
var events = this._events[name];
for(var i = 0; i < events.length; i++){
events[0].callback.call(events[0].ctx,opts);
}
}
}
var eventSplitter = /\s+/;
var View = Youmi.View = function(options){
this.setElement();
this.delegateEvents();
options || (options = {}); // 配置的可选项
_.extend(this, _.pick(options, ‘model‘))
this.initialize.apply(this, arguments);
}
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
_.extend(View.prototype,Events,{
initialize: function(){},
render: function() {
return this;
},
delegateEvents: function(events) {
if (!(events || (events = _.result(this, ‘events‘)))) return this;
// this.undelegateEvents();
for (var key in events) {
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) continue;
var match = key.match(delegateEventSplitter);
var eventName = match[1], selector = match[2];
method = _.bind(method, this);
eventName += ‘.delegateEvents‘;
//$(selector).on(eventName, method);
this.$el.on(eventName, selector, method);
}
return this;
},
undelegateEvents: function() {
this.$el.off(‘.delegateEvents‘ + this.cid);
return this;
},
setElement: function(){
this.$el = this.el ? $(this.el) : $("body");
this.el = this.$el[0];
}
});
var Model = Youmi.Model = function(attributes,options){
var attrs = attributes || {}; // 数据
options || (options = {}); // 配置的可选项
this.changed = {};// 记录当前model有那些东西更改了
this.initialize.apply(this, arguments); // 初始化
}
_.extend(Model.prototype, Events, {
initialize: function(){},
toJSON: function(options) {
return _.clone(this.attributes);
},
set:function(key,val){
var attrs,next,curr;
if( typeof key == "object"){
attrs = key;
}else{
(attrs = {})[key] = val;
}
curr = this.attrs;
next = _.extend({},curr,attrs);
this.attrs = next;
return this;
},
get:function(attr){
return this.attrs[attr];
},
has: function(attr) {
return this.get(attr) != null;
}
})
var extend = function(protoProps, staticProps) {
var parent = this;
var child;
if (protoProps && _.has(protoProps, ‘constructor‘)) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
_.extend(child, parent, staticProps);
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
if (protoProps) _.extend(child.prototype, protoProps);
child.__super__ = parent.prototype;
return child;
};
/*
var extend = function(protoProps, staticProps) {
var parent = this;
var child;
if (protoProps && _.has(protoProps, ‘constructor‘)) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
_.extend(child, parent, staticProps);
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
if (protoProps) _.extend(child.prototype, protoProps);
child.__super__ = parent.prototype;
return child;
};*/
Model.extend = View.extend = extend;
})()
原文:https://www.cnblogs.com/careyyibu/p/9395264.html