我正在尝试使用 BB.js 构建一个小型应用程序。
当然,一切都可以在 FF、CHROME 和 Opera 中运行,但不能在 IE 中运行。
我只是想使用 Restful(php 后端)获取模型以获取模型集合。
在 IE 中,即使多次刷新也没有任何反应。但是当我打开开发工具检查控制台并进行刷新时,它突然起作用了。
模型与收藏
(function($) {
//a fact model
window.Fact = Backbone.Model.extend({
defaults: {
factContent: ''
},
initialize: function Fact(){
console.log("just created a fact");
this.url = "fact.php?fact="+this.id,
this.bind("error", function(model, error){
console.log(error);
});
},
parse : function(resp, xhr) {
//new fact added
if(resp.type == "create")
this.url = "fact.php?fact="+resp.id;
return resp;
}
});
//collection of models
window.Facts = Backbone.Collection.extend({
model: Fact,
url: "facts.php",
initialize: function(){
console.log('fact collection created');
}
});
//facts view
window.FactsCollectionView = Backbone.View.extend({
el: $("#factsCollectionContainer"),
initialize: function(){
this.template = _.template($('#factsCollectionTemplate').html());
//binding
_.bindAll(this, 'render');
this.collection.bind('change', this.render);
this.collection.bind('add', this.render);
this.collection.bind('remove', this.render);
this.collection.bind('reset', this.render);
},
render: function(){
var renderedContent = this.template({facts : this.collection.toJSON()});
$(this.el).html(renderedContent);
return this;
}
});
$(document).ready(function(){
//create a fact collection and populate it
factsc = new Facts();
//NOT WORKING IN IE (no alerts)
//WORKING ONLY USING DEV TOOL
factsc.fetch({success:function(){
//create a view and show collection after fetch is done
factsView = new FactsCollectionView({collection:factsc});
factsView.render();
alert("success fetch");
}, error: function(){
alert("error fetch");
}});
});
})(jQuery);
获取返回此 JSON: [{"id":"48","factContent":"Hello"},{"id":"47","factContent":"World"}]