为什么pulk()和where()方法不起作用
Why pluck() and where() methods not working?
为什么pluck()
和where()
方法不起作用?
var Model = Backbone.Model.extend({
name:'sasha',
time:'now',id:67
})
var model = new Model;
var Model2 = Backbone.Model.extend({
name:'andrii',
time:'now',id:45
})
var model2 = new Model2;
var Model3 = Backbone.Model.extend({
name:'sergii',
time:'now',id:7
})
var model3 = new Model3;
var Library = Backbone.Collection.extend({
initialize:function(){console.log('init')}
})
var lib = new Library([model]);
lib.on('add',function(){
console.log('add event')
})
lib.on('reset',function(){
console.log('reset event')
})
lib.on('remove',function(){
console.log('rremove event')
})
lib.push(model);
lib.push(model2);
lib.push(model3);
pluck
方法在应该返回用户名的情况下返回undefined
值的数组。
var names = lib.pluck("name");//[undefined, undefined, undefined]
并且where
返回一个空数组,而不是id为7的模型。
var wh = lib.where({id:7}) //[]
这不是模型的工作方式。您应该使用Backbone.Model.extend
来定义模型结构,然后创建它的新实例来表示数据。
var Person = Backbone.Model.extend({
/* your custom methods here */
});
var person1 = new Person({
id: 67,
name: 'sasha'
});
var person2 = new Person({
id: 45,
name: 'andrii'
});
var person3 = new Person({
id: 7,
name: 'sergii'
});
创建集合时,可以定义使用的模型:
var Library = Backbone.Collection.extend({
model: Person
});
var lib = new Library();
lib.push(person1);
lib.push(person2);
lib.push(person3);
使用它应该按预期工作:
var names = lib.pluck('name'); // ["sasha", "andrii", "sergii"]
var wh = lib.where({id: 7}); // [{"id": 7, ...}]
相关文章:
- Meteor-添加用户自定义字段的方法不起作用
- 在 JQUERY 中创建 HTML 后,Jquery 方法不起作用
- 为什么这个扩展方法不起作用
- Splice()方法不起作用
- Angularjs-向工厂传递数据的方法不起作用
- Knockout和bread.js的take方法不起作用Paging
- javascript注入$http-post方法不起作用
- 为什么pulk()和where()方法不起作用
- Ajax get 方法不起作用
- 当我将javascript代码放在外部站点.js文件中时,Ajax页面方法不起作用
- getJSON 方法不起作用
- Jquery ajax get 方法不起作用
- jQuery AddClass 方法不起作用
- ScriptManager.RegisterStartupScript() 方法不起作用 - ASP.NET,C#
- 为什么JavaScript方法不起作用
- 为什么 findRandom() mongoose for node.js 方法不起作用
- BackboneJS的设置方法不起作用
- 从javascript调用 asp.net 页面方法不起作用
- 主干.js对以前的方法不起作用进行建模
- toExponential() 方法不起作用