Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 视图行为笨拙_Javascript_Backbone.js_View - Fatal编程技术网

Javascript 视图行为笨拙

Javascript 视图行为笨拙,javascript,backbone.js,view,Javascript,Backbone.js,View,考虑Backbone.js源代码的一部分: // List of view options to be merged as properties. var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']; var View = Backbone.View = function(options) { this.cid = _.uniqueId

考虑Backbone.js源代码的一部分:

// List of view options to be merged as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];

var View = Backbone.View = function(options) {
    this.cid = _.uniqueId('view');
    options || (options = {});
    _.extend(this, _.pick(options, viewOptions));
    this._ensureElement();
    this.initialize.apply(this, arguments);
    this.delegateEvents();
};
我想知道为什么视图实例只分配了“模型”、“集合”、“el”、“id”、“属性”、“类名”、“标记名”、“事件”。我觉得这很烦人。我通常需要发送一些额外的参数。是否可以替换u.extendthis、u.pickoptions、viewOptions;具有
_.扩展此选项

为什么不将额外数据存储在属性散列中

如果确实要直接在视图上设置其他属性,也可以将其作为属性选项传入,然后在initialize方法中的视图对象上设置:

var MyView = Backbone.View.extend({
    initialize: function() {
        var self = this;

        if (this.attributes) {
            _.each(this.attributes, function(val, key) {
                self[key] = val;
            });
        }
    }
});

var view = new MyView({ attributes: { foo: 'bar' } });

事实上,我现在正在使用类似的代码。我想知道var viewOptions=['model','collection','el','id','attributes','className','tagName','events']背后的原因;啊。是的,我不知道白名单背后的原因。也许是为了保持顶级视图属性的整洁。或者使用attributes属性在视图中存储任意数据是为了与模型的实现方式保持一致?我想我的倾向通常只是避免修改第三方源代码: