码迷,mamicode.com
首页 > 其他好文 > 详细

简洁的前后端分类框架

时间:2018-07-31 15:18:35      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:back   attribute   sele   str   win   render   cto   spl   view   

(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;


})()

  

简洁的前后端分类框架

标签:back   attribute   sele   str   win   render   cto   spl   view   

原文地址:https://www.cnblogs.com/careyyibu/p/9395264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!