Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 取消设置以相同子字符串开头的Backbone.js模型的所有属性_Javascript_Backbone.js_Model_Underscore.js - Fatal编程技术网

Javascript 取消设置以相同子字符串开头的Backbone.js模型的所有属性

Javascript 取消设置以相同子字符串开头的Backbone.js模型的所有属性,javascript,backbone.js,model,underscore.js,Javascript,Backbone.js,Model,Underscore.js,我想取消设置以相同子字符串开头的Backbone.js模型的所有属性,如: logo_id logo_width logo_height 我想做一些类似的事情: model.unsetAllStartsWith('logo'); _.extend(Backbone.Model, { unsetAllStartsWith : function(str){ // something with this.toJSON() ? } }); 代码类似于: model.

我想取消设置以相同子字符串开头的Backbone.js模型的所有属性,如:

logo_id
logo_width
logo_height
我想做一些类似的事情:

model.unsetAllStartsWith('logo');
_.extend(Backbone.Model, {
    unsetAllStartsWith : function(str){
        // something with this.toJSON() ?
    }
});
代码类似于:

model.unsetAllStartsWith('logo');
_.extend(Backbone.Model, {
    unsetAllStartsWith : function(str){
        // something with this.toJSON() ?
    }
});

我对模型和toJSON感到困惑。另外,这是扩展主干模型类的正确方法吗?

Backbone.model内置了扩展方法,因此您可以执行以下操作:

var MyModel = Backbone.Model.extend({
    unsetAllStartsWith: function(str){
       // get clone of attributes to iterate over
       var attrs = _.clone(this.attributes);
       _.each(attrs, function(val, key){
              if(key.indexOf(str) == 0){
                  this.unset(key);
              }
           }, this);
    }
});

var model = new MyModel({logo_width: 100, logo_height: 200});
model.unsetAllStartsWith('logo');
试试这个

unsetAllStartsWith : function(str){ var $this = this var properties = Object.keys($this.toJSON()); _.each(properties, function(property) { var regex = new RegExp('^'+ str, 'gi') if(property.match(regex)) $this.unset(property) } } } unsetAllStartWith:函数(str){ var$this=this var properties=Object.keys($this.toJSON()); _.每个(属性、功能(属性){ var regex=new RegExp(“^”+str,“gi”) if(property.match(regex)) $this.unset(属性) } } }
对于添加方法(),这里有一个稍微不同的观点:

像这样使用:

var MyModel = Backbone.Model.extend({
    defaults: {
        'foo_first': 'bar',
        'foo_second': 'bar2',
        'different': 'bar3'
    }
});

var testModel = new MyModel();
testModel.unsetAllStartsWith('foo');