backbone.js - 将jquery插件应用于动态创建的对象

时间:2015-04-27 05:20:16

标签: javascript jquery backbone.js

我有类.knob的动态创建输入的集合,我需要应用knob.js插件$(".knob").knob();但是当我调用函数时DOM是空的。
问题是 - 当我必须调用插件时,哪种方式更好?

我的模板

<script id="UserProjectTemplate" type="text/template">
    ...
    <input class="knob" data-angleOffset=90 data-linecap=round value="35">
</script>

列出项目视图

 var UserProjectsItemView = Backbone.View.extend({
        ...
        template: _.template($("#UserProjectTemplate").html()),
        render: function () {
            $(this.$el).append();
            $(this.$el).html(this.template(this.model.toJSON()));
            return this;
        }
});

集合

var UserProjectsCollection = Backbone.Collection.extend({
    ...
    url: function () {
        return 'service/api.php/users/' + this.user_id + '/projects';
    },
});

查看收集

var UserProjectsListView = Backbone.View.extend({
    ...
    initialize: function () {
        this.collection = new UserProjectsCollection(null, {user_id: 5});
        this.collection.fetch({reset: true});

        this.collection.on("reset", this.render, this);
    },

    render: function () {
        var that = this;
        _.each(this.collection.models, function (item) {
            that.renderProject(item);
        });
    },

    renderProject: function (item) {
        var itemProjectView = new UserProjectsItemView({
            model: item
        });
        this.$el.append(itemProjectView.render().el);
    }
});

0 个答案:

没有答案