emberjs应该在哪里准备好函数? [英] emberjs where should document ready functions go?

查看:112
本文介绍了emberjs应该在哪里准备好函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 typeahead 附加到我的一个模板中的文本输入。因为Ember使用的手柄,jQuery的文档就绪功能不是类型头部定义的地方。放置模板准备代码的正确位置在哪里?我尝试了一个控制器,但没有来自头脑的回应。我不认为模板是否被渲染。

I'm trying to attach a typeahead to a text input in one of my templates. Because Ember use's handlebars, jQuery's document ready function is not the place for the typeahead definition. Where is the proper place to put "template ready" code? I tried a controller but there was no response from the typeahead. I don't think the template was rendered yet.

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);
        }
    }
});


推荐答案

正确的地方是 didInsertElement Ember.View

例如:

模板

<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'
  }
});

这篇关于emberjs应该在哪里准备好函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆