モデルを使用していない理由がわかりません。あなたの質問に答えると、さまざまな解決策があります。最初の解決策は次のとおりです。
イベントの使用:
app.SomeView = Backbone.View.extend({
render: function() {
var that = this;
$.getJSON(someURL, function(result) {
that.property = result.something;
that.trigger('DataLoaded', that);
});
return this;
}
});
var view = new app.SomeView();
view.on('DataLoaded', function(theView){
console.log( theView );
});
2 つ目は、コールバックを追加して渡す必要があります。
app.SomeView = Backbone.View.extend({
render: function(callback) {
var that = this;
$.getJSON(someURL, function(result) {
that.property = result.something;
callback(that);
});
return this;
}
});
var view = new app.SomeView();
view.render( function(theView){
console.log( theView );
});
私の回答は、あなたが作成した質問を修正するために書かれました。しかし、長期的な改善のために、Models には基本的にサーバーから JSON をロードしてそれを Model に関連付ける fetch メソッドがあることをご存知ですか?
http://backbonejs.org/#Model-fetch
JSON を読み込む方法は次のとおりです。
app.SomeModel = Backbone.Model.extend({
urlRoot : someURL
});
app.SomeView = Backbone.View.extend({
initialize : function(){
this.model.on('change', this.render);
},
render: function() {
console.log( this.model.toJSON() );
return this;
}
});
var view = new app.SomeView(new app.SomeModel());
view.model.fetch();