Javascript can';t使用backbone.js正确获取集合
请一直读到最后(最后我参考了console.log) 模型:Javascript can';t使用backbone.js正确获取集合,javascript,backbone.js,backbone.js-collections,Javascript,Backbone.js,Backbone.js Collections,请一直读到最后(最后我参考了console.log) 模型: // Spot model: define([ 'jquery', 'underscore', 'backbone' ], function($, _, Backbone){ var Spot = Backbone.Model.extend({ url: function(){ if (this.get('id') !== null){
// Spot model:
define([
'jquery',
'underscore',
'backbone'
], function($, _, Backbone){
var Spot = Backbone.Model.extend({
url: function(){
if (this.get('id') !== null){
return '/spot/' + this.get('id');
}
return '/spots';
},
idAttribute: "id",
defaults: {
id: null,
details:{
name: "school",
type: 4,
rank: 3,
comment: null
},
location:{
address: '',
lat: 70.345,
lng: 90.123
},
user:{
id: 12345,
name: "magneto"
}
},
parse: function(response){
/* var attrs = {
details: {},
location: {},
user: {}
};
attrs.id = response.id;
attrs.details.name = response.details.name;
attrs.details.type = response.details.type;
attrs.details.rank = response.details.rank;
attrs.location.lat = response.location.lat;
attrs.location.lng = response.location.lng;
return attrs; */
}
});
return Spot;
});
收藏:
// Spots collection:
define([
'jquery',
'underscore',
'backbone',
'models/Spot'
], function($, _, Backbone,spot_model){
var Spots = Backbone.Collection.extend({
model: spot_model,
url:
function() {
return '/projects/FE/spots';
},
parse: function(response){
/* var parsed = [],
key;
_.each(response, function (value) {
parsed.push(value);
}, this);
return parsed;*/
},
comparator: function(spot){
return spot.rank;
}
});
return Spots;
});
观点:
// inside our view named: SpotsView
define([
'jquery',
'underscore',
'backbone',
'mustache',
'icanhaz',
'views/spots/Spot',
'collections/Spots',
'text!../../../../templates/spots/spots.mustache!strip',
], function($,
_,
Backbone,
mustache,
ich,
SpotView,
Spots,
SpotsTemplate){
var SpotsView = Backbone.View.extend({
//el: $("#container"),
tagName: "ul",
initialize: function(){
console.log('initialize view');
console.log(this.collection);
_.bindAll(this,'render');
},
render: function(){
console.log(this.collection);
console.log('length: ' + this.collection.length);
console.log('models: ' + this.collection.models);
_.each(this.collection.models, function (spot) {
$(this.el).append(new SpotView({model:spot}).render().el);
}, this);
return this;
}
});
return SpotsView;
});
在我们的app.js中
var spots = new Spots({model: Spot}),
spotsview;
spots.reset();
console.log(spots.fetch());
console.log('spots:');
console.log(spots.length);
console.log(spots);
spotsview = new SpotsView({collection: spots});
服务器输出
// SERVER output(s) i tried:
{"id": 666,"details":{"name": "mark","type":4,"rank":3.1,"comment":"nothing"},"location":{"lat":70.123,"lng":90.321,"address":"5th avenue"},"user":{"id":333,"name":"wolverine"}}
// another one i tried:
[{"id": 666,"details":{"name": "mark","type":4,"rank":3.1,"comment":"nothing"},"location":{"lat":70.123,"lng":90.321,"address":"5th avenue"},"user":{"id":333,"name":"wolverine"}}]
// another one:
[{"id":"55","details":{"name":"Dan","type":"6","rank":"-9.9","comment":"myEx"},"location":{"lat":"40.780346","lng":"-73.957657","address":"78, West 86th Stree"}},{"id":"57","details":{"name":"Ron","type":"6","rank":"3.0","comment":"Butch"},"location":{"lat":"40.715569","lng":"-73.832428","address":"1344 Queens Boulevard"}},{"id":"58","details":{"name":"Monk's","type":"11","rank":"9.5","comment":"yesss"},"location":{"lat":"40.805443","lng":"-73.965561","address":"112th and broadway "}}]
// without using "parse" method in the collection and in the model (they are commented) i get:
d
_byCid: Object
_byId: Object
length: 0
models: Array[0]
__proto__: x
// when not commented (parse in both collection and model) i get:
_byCid: Object
_byId: Object
length: 6
models: Array[6]
__proto__: x
// despite it says 6, in the view you can see there are lines:
//console.log(this.collection); <--- returns the abovee
//console.log('length: ' + this.collection.length); <-- returns 0
//console.log('models: ' + this.collection.models); <--- none
//我尝试的服务器输出:
{“id”:666,“详细信息”:{“姓名”:“标记”,“类型”:4,“等级”:3.1,“评论”:“无”,位置:{“lat”:70.123,“lng”:90.321,“地址:”第五大道“}”,用户:{“id”:333,“姓名”:“狼獾”}
//我试过的另一个:
[{“id”:666,“细节”:{“姓名”:“标记”,“类型”:4,“等级”:3.1,“评论”:“无”,位置:{“lat”:70.123,“lng”:90.321,地址:““第五大道”},用户:{“id”:333,“姓名”:“狼獾”}]
//另一个:
地址:{lat:“40.715569”,“lng:”--73.832428,“地址:”{id:“西86街78号”},{id:“57”,“细节:{”姓名:“:“罗恩”,“类型:”:“6”,“等级:”:“3.0”,“评论:”:“布奇”},位置:{“拉特:”:“40.715569”,“lng:“--73.832428”,“地址:”:“1344大道”{{“姓名”:“Monk's”,“类型”:“11”,“等级”:“9.5”,“评论”:“yesss”},“地点”:{“lat”:“40.805443”,“lng”:“-73.965561”,“地址”:“112街和百老汇”}]
//如果不在集合和模型中使用“parse”方法(它们被注释),我会得到:
D
_byCid:对象
_byId:Object
长度:0
型号:阵列[0]
__原型:x
//未注释时(在集合和模型中解析),我得到:
_byCid:对象
_byId:Object
长度:6
型号:阵列[6]
__原型:x
//尽管上面写着6,但在视图中您可以看到以下几行:
//console.log(this.collection);collection.fetch
是一个异步操作,因此当您获取集合时,将发送ajax调用,然后您的代码将继续运行,就像什么都没有发生一样
spots.reset(); // emptied your collections
console.log(spots.fetch()); // fetch your collection (why is this inside a console.log???)
console.log('spots:'); // log some text, this will be executed right after your fetch call has been made
console.log(spots.length); // here the fetch call probably hasn't returned yet, so returns 0
console.log(spots); // same as above, so returns an empty collection
spotsview = new SpotsView({collection: spots}); // the collection might or might not get populated by the time you get to rendering so there is probably variation from program run to another
那么如何解决这个问题呢?在视图中,将呈现函数绑定到集合的重置事件(在每次成功获取或强制重置后启动)。这样,视图只有在有东西要显示时才会呈现
// view's initialize method
initialize: function() {
...
this.collection.on('reset', this.render);
...
}
我可能已经解决了这个问题:我甚至已经从模型和集合中删除了“parse”声明,并且成功了:长度:6 SpotsView.js models:[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象],[对象对象对象对象]无论是哪种方式,我都想知道正确的用法,若我做得对,以及如何在这两种情况下(集合和模型方面)正确地使用PARSE,你们会在这两种(!)中返回什么。tnxjake:tnx对于输入,我实际上已经用this.collection.bind('all',this.render)绑定了集合的视图;在播放时。无论哪种方式,您都可以看到上面,我能够解决问题。但是关于在这两种情况下使用解析(模型和集合仍然是个谜)。谢谢。
// view's initialize method
initialize: function() {
...
this.collection.on('reset', this.render);
...
}