2013-10-27 44 views
3

我正在嘗試將typeahead附加到我的一個模板中的文本輸入。由於Ember使用的句柄,jQuery的文檔就緒函數不是typeahead定義的地方。適合放置「模板準備好」代碼的地方在哪裏?我嘗試了一個控制器,但是沒有任何迴應。我不認爲模板已經呈現。emberjs應該在哪裏文檔準備好的功能去?

App.PersonController = Ember.ObjectController.extend({ 

    isEditing: false, 

    init: function(){ 
     this._super(); 

     $('.example-films .typeaheadcx').typeahead([{ 
       name: 'best-picture-winners', 
       remote: 'http://twitter.github.io/typeahead.js/data/films/queries/%QUERY.json', 
       prefetch: 'http://twitter.github.io/typeahead.js/data/films/post_1960.json', 
       template: '<p><strong>{{value}}</strong> â€「 {{year}}</p>', 
       engine: Ember.Handlebars 
     }]); 
    }, 

    actions: { 
     edit: function() { 
      this.set('isEditing', true); 
     }, 

     doneEditing: function() { 
      this.set('isEditing', false); 
     } 
    } 
}); 

回答

5

的正確位置應該是Ember.ViewdidInsertElement

例如:

模板

<script type="text/x-handlebars" data-template-name="foo"> 
    Hello world 
</script> 

查看

App.FooView = Ember.View.extend({ 
    templateName: 'foo', 
    didInsertElement: function() { 
    console.log(this.$().text()); // will log 'hello world' 
    } 
}); 
+0

感謝...這似乎是正確的答案,但如果我試圖做到這一點在部分?我的模板名稱是「show/_edit」。似乎打破了路由。 https://bitbucket.org/dadamssg/embercrm/src – David

+0

對不起,但是,我看不到你的倉庫中的'show/_edit'。部分是非常簡單的,不可能創建一個部分的視圖。如果你理解你的問題,你將在表單中使用打頭的一些輸入。我的建議是創建一些ember組件或Ember.TextField子類,並在didInsertElement中使用typehead。因此,在該自定義視圖中,您將使用綁定傳遞值,如{{App.TypeHead valueBinding =「category」...}}。你會有在任何模板中使用它的優點:) –

+0

甜。我繼續前進,並創建了一個Viewahead的視圖。它工作得很好。謝謝您的幫助! – David