marionette compositeview onfetch

时间:2013-03-27 15:51:27

标签: javascript backbone.js marionette

我有一个骨干牵线木偶合成视图如下。

这是初始化方法。

initialize: function(options) {

        log.debug("Initialize");
        this.wizard = options.wizard;

        this.model = new Feed({
            id: options.modelid
        });
        this.modelid = options.modelid;
        this.collection = new Similar();

        this.listenTo(this.model, "change", this.onFetch);
        this.listenTo(this.model, "reset", this.onFetch);

        this.collection.fetch({ data: { id: this.modelid }});

        this.model.fetch();
    },

这是onFetch方法

onFetch: function() {
        log.debug("Fetch");

        this.$el.html(this.template(this.model.toJSON()));}

这是追加方法

appendHtml: function(collectionView, itemView, index) {
        log.debug("appendHtml");

        var jsonFeed = itemView.model.toJSON();

        if (this.prevFeed === null || jsonFeed.start !== this.prevFeed.start) {
            var date = new Date(jsonFeed.start);
            collectionView.$(this.itemViewContainer).append('<div class="day-divider c-color box-center" ><h3>' + moment(date).format('DD MMMM') + '</h3></div>');
        }

        collectionView.$(this.itemViewContainer).append(itemView.el);

        this.prevFeed = jsonFeed;
    },

我的集合被覆盖html内容的onFetch函数清空。 这是我的日志:

DEBUG - Initialize
DEBUG - Render
DEBUG - appendHtml
DEBUG - appendHtml
DEBUG - appendHtml
DEBUG - Render
DEBUG - Fetch

知道这是怎么发生的吗? 我怎么解决?

1 个答案:

答案 0 :(得分:0)

为什么不使用onRetch的onRender instad? onRender是一个已经包含的函数,在渲染完成后显示内容。

注意:你甚至可以使用onShow来完成所有工作。