具有Q.promise问题的主干模式

时间:2015-11-13 14:46:53

标签: backbone.js modal-dialog promise q

我们有一个方法(onOpenNotitiesClicked),用于显示用于输入注释的模态视图。我们为此实现了Backbone Modal插件(https://github.com/awkward/backbone.modal)。

有两种情况:

  1. 后端还没有注释:初始化并渲染
    模态

  2. 后端已有笔记=>先收集它们 然后将注释传递给模态(初始化),然后渲染

  3. 在第一种情况下它工作正常!显示模态。
    在第二种情况下,模态未显示。
    我调试了这两种情况,在两种情况下都执行了alle方法,在元素中,我看到了模态视图的HTML。

    enter image description here

    我怀疑this在Q / promise数据获取过程中丢失了一些数据,但我看不清楚/在哪里/如何/为什么......

    任何人都知道我做错了什么?或者我错过了什么?

    创建模态:

        onOpenNotitieClicked: function (event) {
            var $element, taak, taakId, id, options = {};
    
            $element = this.$(event.currentTarget).closest("li");
            id = $element.data("id");
            taakId = $element.data("taak-id");
            taak = this.getCurrentTask(event);
    
            options.taakKey = id;
            options.taakId = taakId;
            options.heeftNotities = taak.heeftNotities;
            options.datacontroller = this.datacontroller;
            this.notitiesOptions = options;
            // this.renderNotitieModal(options);
            if (taak.heeftNotities) {
                this.getActiviteitNotities(taakId).then(_.bind(this.onSuccessGetNotities, this), _.bind(this.onErrorGetNotities, this));
            } else {
                this.renderNotitieModal(this.notitiesOptions);
            }
    
        },
    

    如果要收集笔记:

        getActiviteitNotities: function (taakId) {
            return this.datacontroller.getNotities(taakId);
        },
    
        onSuccessGetNotities: function (notities) {
            this.notitiesOptions.notities = notities;
            this.renderNotitieModal(this.notitiesOptions);
        },
    
        onErrorGetNotities: function () {
            this.renderNotitieModal(this.notitiesOptions);
        },
    

    要从后端获取备注,请使用Q / promises。

        getNotities: function (taakId, refresh, klantorderId) {
            return Q.promise(function (resolve, reject) {
                var options = {};
    
                if (!this.notitiesCollection || this.taakId !== taakId || refresh) {
                    delete this.notitiesCollection;
    
                    this.notitiesCollection = this.createCollection("NotitiesCollection", {
                        id: this.taakId,
                        resource: this.NOTITIES_RESOURCE
                    });
    
                    if (taakId) {
                        this.taakId = taakId;
    
                        options = {
                            data: {
                                parentId: this.taakId
                            }
                        };
                    } else if (klantorderId) {
    
                        options = {
                            data: {
                                klantorderId: klantorderId
                            }
                        };
                    }
    
                    resolve(this.notitiesCollection.fetch(options));
                } else if (this.notitiesCollection) {
                    resolve(this.notitiesCollection.toJSON());
                } else {
                    reject("ERROR");
                }
            }.bind(this));
        },
    

    Notities.js(模态视图):

    (function () {
    "use strict";
    
    App.Bewaking.modals.BewakingNotitieModal = Backbone.Modal.extend({
        template: JST.bewaking_notitie_modal, //jshint ignore:line
        title: "Notities",
        formatter: new App.Main.helpers.Formatter(),
    
        events: {
            "click #save-notitie": "onSaveNotitieClicked"
        },
    
        initialize: function (options) {
            this.taakId = options.taakId;
            this.taakKey = options.taakKey;
            this.datacontroller = options.datacontroller;
            this.notities = options.notities;
        },
    
        afterRender: function () {
            console.log("afterRender");
            this.$notitieModal = this.$("#notitieModal");
            this.$nieuweNotitie = this.$("#nieuwe-notitie");
            this.$notitieErrorTekst = this.$("#notitie-error-tekst");
    
            this.$notitieModal.on("shown.bs.modal", function () {
                this.$nieuweNotitie.focus();
            }.bind(this));
        },
    
        render: function () {
            console.log(this.notities);
            this.$el.html(this.template({
                formatter: this.formatter,
                notities: this.notities
            }));
    
            return this;
        }
     });
    }());
    

0 个答案:

没有答案