nl_0の答えが言ったように。ajax 呼び出しから約束を返しています。返したいもの、または返すことを意図したもの@
は、子ビュー インスタンスです。nl_0 は、データのフェッチ方法についても正しいです。バックボーン モデルとコレクション データ レイヤーを使用するのではなく、ajax 呼び出しからデータをフェッチする場合、バックボーンを実際に使用しているわけではありません。そのため、彼の回答に投票するつもりですが、以下が問題の説明に役立つことを願っています.
何が起こっているのですか:
class ParentView extends Backbone.View
el:"#main"
childView : new ChildView
render: =>
$(@el).empty().append(@childView.render().el)
// because of the subtle mistakes in @childView.render() below
// your childView never got anything appended to it.
// @childView.render().el is undefined
// essentially $(@el).empty().append(undefined)
// that's why nothing shows up.
class ChildView extends Backbone.View
render: =>
$.ajax
url:"get_data"
success: (data) =>
// @ is not a reference to your child view.
// the value of @ has changed context
// that means @el is undefined.
// You're selecting $(undefined) not your views element.
$(@el).empty().append("Child View done")
@ // this is not your ChildView instance
あなたができることは次のとおりです。
class ParentView extends Backbone.View
el:"#main"
childView : new ChildView
render: =>
// this line is actually okay if we fix @childView.
$(@el).empty().append(@childView.render().el)
class ChildView extends Backbone.View
onSuccess: (data) =>
// the double arrow binds this function to the proper context
// @ is our ChildView instance
@$el.empty().append("GET get_data is done.")
render: =>
$.ajax
url:"get_data"
success: @onSuccess
// return @ out here where it is the instance.
// This is really where "Child View done".
@
間違いがありましたら申し訳ありません。私のコーヒースクリプトのスキルはかなり使われていません。これがあなたの言いたいことだと思います。非常に微妙なことです。この回答を編集する必要がある場合はお知らせください。
childView.render().el
ajax 呼び出しにかかる時間によっては、dom に追加されるため、ajax 呼び出しが戻ってきて "GET get_data is done" を追加する前に、わずかな遅延が発生する場合があります。