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"}]