我有一个像这样的Backbone Collection:
var ThreadCollection = Backbone.Collection.extend({
url: '/api/rest/thread/getList'
});
var myCollection = new ThreadCollection();
Run Code Online (Sandbox Code Playgroud)
然后我使用数据对象从服务器获取它以附加查询参数(所以在这种情况下它出来'/ api/rest/thread/getList?userId = 487343')
myCollection.fetch({
data: {
userId: 487343
}
})
Run Code Online (Sandbox Code Playgroud)
我可能想要使用其他参数而不是userId(groupId,orgId等),但我理想情况下在初始化时定义数据参数,然后才能运行fetch()而不指定.像这样的东西:
var myCollection = new ThreadCollection({
data: {
userId: 487343
}
});
myCollection.fetch()
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有谁知道有没有办法做到这一点?谢谢!
我正在使用Backbone.Marionette的请求 - 响应框架来获取集合中的数据,然后将其响应给请求它的请求对象,但显然它不会等待集合被填充.这是我的代码:
这是它请求数据的地方:
// Module: Timeline, ListController
var employees = App.request('employee:timeline');
Run Code Online (Sandbox Code Playgroud)
这是我设置我的处理程序的地方:
// Entities Module
App.reqres.setHandler('employee:timeline', function() {
return API.getEmployeesForTimeline();
});
Run Code Online (Sandbox Code Playgroud)
这是我的API的功能:
getEmployeesForTimeline: function() {
var employees = new Entities.EmployeeCollection();
employees.fetch({
success: function(employees) {
returnEmployees(employees);
}
});
function returnEmployees(employees) {
// doing some things with employees collection
return leaves;
}
}
Run Code Online (Sandbox Code Playgroud) javascript fetch backbone.js backbone.js-collections marionette
似乎我无法遍历骨干集合.我已经在几个主题中看过这个主题,但这些解决方案都没有帮助.
render:function () {
this.$el.html(this.template(this.model.attributes));
var that = this;
console.log(this.projects.models);
_.each(this.projects.models, function(model) { console.log(model); });
return this;
}
Run Code Online (Sandbox Code Playgroud)
从这个我的控制台只显示数组[2]我希望看到每个模型.有谁知道我在这里做错了什么?
加载视图时,收集长度始终返回1,而解析函数中当前显示3个成员.我想知道问题是否是嵌套项目视图,但似乎没有相同的行为!我不明白为什么self.push(成员)不会将模型添加到集合中!有点困在这里,有什么帮助吗?
该模型
define([
'backbone'
], function(Backbone) {
'use strict';
var MemberModel = Backbone.Model.extend({
id: "_id"
});
return MemberModel;
});
Run Code Online (Sandbox Code Playgroud)
这个系列
define([
'backbone',
'models/MemberModel'
], function(Backbone, MemberModel) {
'use strict';
var Members = Backbone.Collection.extend({
model: MemberModel,
url: '/api/v1/users',
parse: function(response, options) {
var self = this;
_.each(response.users, function(item){
var member = new self.model();
member.set('email', item.email);
member.set('firstname', item.firstname);
member.set('lastname', item.lastname);
member.set('group', item.group);
member.set('city', item.city);
// shows every member's emails
console.log('member.email='+member.get('email'));
self.push(member);
});
console.log('this.length='+this.length); // this is always equal to 1
return this.models; …Run Code Online (Sandbox Code Playgroud) requirejs backbone.js backbone-views backbone.js-collections
我有如下JSON响应
{
"results": [
{
"name": "FOO",
"containerName": "Foo",
"accounts": [
{
"id": "10445570_7601",
"shareeAccountInfo": "",
"siteAccountId": "271555",
"siteId": "271555",
"refreshMode": "NORMAL",
"isNetIncl": "true",
"propertyId": null,
"amount": [
"0.0",
"USD"
]
},
{
"id": "1070_20537601",
"shareeAccountInfo": "",
"siteAccountId": "271555",
"siteId": "271555",
"refreshMode": "NORMAL",
"isNetIncl": "true",
"propertyId": null,
"amount": [
"0.0",
"USD"
]
}
]
},
{
"name": "FOO123",
"containerName": "Foo123",
"accounts": [
{
"id": "10445570_20601",
"shareeAccountInfo": "",
"siteAccountId": "271555",
"siteId": "271555",
"refreshMode": "NORMAL",
"isNetIncl": "true",
"propertyId": null,
"amount": [
"0.0", …Run Code Online (Sandbox Code Playgroud) 我有一个单页应用程序使用骨干来管理模型集合.
有时这个集合变得非常大,如果用户执行的操作会改变很多,这会导致.save被多次调用.在某些情况下,导致数百个ajax请求同时触发.
骨干网是否为这样的批量操作提供了单一的请求?或者是否有首选模式可供使用?
谢谢你的建议.
javascript ajax backbone.js backbone-events backbone.js-collections
我的Collection收集了一些记录,我只需要显示前10条记录.我试过了
this.collection.each(function(){
if (count == 10) break;
//pass model to view
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,break不能用于underscore.js的each()API.请参考这里:如何在underscore.js中打破_.each函数
如何编写一个过滤器,只从集合中选择前十名
this.collection.filter();
Run Code Online (Sandbox Code Playgroud)
更新:collection.first(10)提取了我的过滤列表.但是,我仍然需要将.each()链接到此集合来处理集合项.collection.first()不允许链.请参阅我选择的答案以获得解决方案.
我有这个漂亮的函数遍历集合中的模型以获取属性和值.如果找到该值则返回true.
在浏览了很多文档之后,我仍然对如何正确遍历集合以及如何搜索它感到困惑.由于underscorejs(在我的情况下是lodash)被挂钩到主干上,我会用.each遍历集合
之后我没有放入其他if (model.get(attribute)===value)因为它会在遍历整个集合之前返回false.回调函数听起来像是不必要的并发症 - 但也许我错了(我几个月前开始使用JS)
我会很高兴提示和/或更好的解决方案;-)与explonation.提前致谢.
我使用requirejs,为什么我通过_,Bacbkone ......
以下是Collection的外观:
function (_, Backbone, AppModels) {
var QueriesCollection = Backbone.Collection.extend({
model : AppModels.QueryModel,
search: function (attribute, value) {
var found = false;
this.each(function (model) {
if (model.get(attribute)===value) {
found = true;
}
});
return found;
}
});
return {
QueriesCollection: QueriesCollection
};
});
Run Code Online (Sandbox Code Playgroud) 这是我的JS代码
var mainApp = new Backbone.Marionette.Application();
mainApp.addRegions({
mainRegion: "#main-region"
});
var UserModel = Backbone.Model.extend();
var UserView = Backbone.Marionette.ItemView.extend({
tagName: 'div',
className: 'user-item',
template: '#user-item-tpl'
});
var UserCollection = Backbone.Collection.extend({
model: UserModel,
sortKey: 'id',
comparator: function(item1, item2){
item1 = item1.get(this.sortKey);
item2 = item2.get(this.sortKey);
return item1 > item2 ? 1 : (item1 < item2 ? -1 : 0);
},
sortByID: function(){
this.sort();
}
});
var UserListView = Backbone.Marionette.CompositeView.extend({
className: 'user-view',
tagName: 'div',
itemView: UserView,
itemViewContainer: '.user-list',
events: {
'click .btn' : 'doSort' …Run Code Online (Sandbox Code Playgroud) sorting backbone.js backbone-views backbone.js-collections marionette
我有从服务器返回的JSON,我正在用它填充Backbone集合.
Person = Backbone.Model.extend({
});
PersonCollection = Backbone.Collection.extend({
model: Person
});
Run Code Online (Sandbox Code Playgroud)
var jsonString = "[
{ \"name\": \"Anna\", \"id\": 5 },
{ \"name\": \"Lina\", \"id\": 26 },
{ \"name\": \"Melissa\", \"id\": 55 }
]"
var people = JSON.parse(jsonString); //people is now an array of 3 persons
Run Code Online (Sandbox Code Playgroud)
var personCollection = new PersonCollection();
for (var i = 0; i < people.length; i++) {
personCollection.add(people[i]);
}
var personCollectionView = new PersonCollectionView({ collection: personCollection});
Run Code Online (Sandbox Code Playgroud)
var personCollectionView = new PersonCollectionView({ collection: …Run Code Online (Sandbox Code Playgroud) 我一次保存一个模型没有问题,我写了一个对象数组的递归保存.在每次成功保存时,我移出一个对象,如果数组长度不是0 - >重复...一旦数组长度达到零,那么我知道所有保存都成功并执行适当的操作.
如果有更好的方法,想知道吗?比上面描述的方法,休息服务api不采用集合,但如果我有一个保存集合的例子,我会要求修改服务.
调用fetch函数时出错.
Undefined is not a function Error in Backbone js
Run Code Online (Sandbox Code Playgroud)
var models = Backbone.Model.extend({
userId: function() {
return this.get('userId');
},
id: function() {
return this.get('id');
},
body: function() {
return this.get('body');
}
});
//Collections
var todolistStore = Backbone.Collection.extend({
url: function() {
return 'https://jsonplaceholder.typicode.com/posts'
},
model: models,
parse: function(response) {
return response;
}
});
todolistStore.fetch();Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>Run Code Online (Sandbox Code Playgroud)