Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript BackboneJS视图渲染问题_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript BackboneJS视图渲染问题

Javascript BackboneJS视图渲染问题,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我有一个详细视图,从那里我加载一个模式弹出窗口,如下所示 var modal = new MyModalView({ model: person }); define([ 'base/BaseModalView' ], function(BaseModalView) { var MyModal = BaseModalView.extend({ el: "#myModalContainer", initialize: function()

我有一个详细视图,从那里我加载一个模式弹出窗口,如下所示

var modal = new MyModalView({
    model: person
});
define([
    'base/BaseModalView'
], function(BaseModalView) {
    var MyModal = BaseModalView.extend({
        el: "#myModalContainer",
        initialize: function() {
            this._initAndRenderModal(myModalTemplate, {
                model: this.model.toJSON()
            });
        },
        render: function() {
            var self = this;
            self.personModel = new Person({
                "personId": $('#personName option:selected').data('personid'),
                "personName": $('#personName').val()
            });

            self.populatePersons();
        },
        populatePersons: function() {

            var self = this,
                    $personName = $('#personName', this.el);

            $personName.attr('disabled', 'disabled').html('<option value="">Loading Persons...</option>');

            self.personCollection.fetch({
                success: function() {
                    var personList = self.personCollection.toJSON(),
                            aOptions = '<option value="">- Select -</option>',
                            l = personList.length;
                    if (l)
                    {
                        for (var i = 0; i < l; ++i)
                        {
                            var val = _.escape(personList[i].personName);
                            aOptions += '<option value="' + _.escape(personList[i].personName) + '" data-personid="' + _.escape(personList[i].personId) + '" title="' + val + '">' + val + '</option>';
                        }
                        $personName.removeAttr('disabled').html(aOptions);
                    }
                    else
                    {
                        $personName.html('<option>No Person available</option>');
                    }

                    if (self.model.get("personId") != null) {
                        $personName.val(self.model.attributes.person.attributes.personName);
                        $personName.attr("disabled", "disabled");
                    }

                }
            });

        },
        saveAction: function(event) {
            var self = this;

            var checkPerson = self.personModel.validateAll();

            var invalid = !checkPerson.isValid;
            if (invalid) {
                if (!checkPerson.isValid) {
                    self.showValidationErrors(checkPerson.messages);
                }
                return true;
            }

            return false;
        }
    });
    return MyModal;
});
我的模态视图如下所示

var modal = new MyModalView({
    model: person
});
define([
    'base/BaseModalView'
], function(BaseModalView) {
    var MyModal = BaseModalView.extend({
        el: "#myModalContainer",
        initialize: function() {
            this._initAndRenderModal(myModalTemplate, {
                model: this.model.toJSON()
            });
        },
        render: function() {
            var self = this;
            self.personModel = new Person({
                "personId": $('#personName option:selected').data('personid'),
                "personName": $('#personName').val()
            });

            self.populatePersons();
        },
        populatePersons: function() {

            var self = this,
                    $personName = $('#personName', this.el);

            $personName.attr('disabled', 'disabled').html('<option value="">Loading Persons...</option>');

            self.personCollection.fetch({
                success: function() {
                    var personList = self.personCollection.toJSON(),
                            aOptions = '<option value="">- Select -</option>',
                            l = personList.length;
                    if (l)
                    {
                        for (var i = 0; i < l; ++i)
                        {
                            var val = _.escape(personList[i].personName);
                            aOptions += '<option value="' + _.escape(personList[i].personName) + '" data-personid="' + _.escape(personList[i].personId) + '" title="' + val + '">' + val + '</option>';
                        }
                        $personName.removeAttr('disabled').html(aOptions);
                    }
                    else
                    {
                        $personName.html('<option>No Person available</option>');
                    }

                    if (self.model.get("personId") != null) {
                        $personName.val(self.model.attributes.person.attributes.personName);
                        $personName.attr("disabled", "disabled");
                    }

                }
            });

        },
        saveAction: function(event) {
            var self = this;

            var checkPerson = self.personModel.validateAll();

            var invalid = !checkPerson.isValid;
            if (invalid) {
                if (!checkPerson.isValid) {
                    self.showValidationErrors(checkPerson.messages);
                }
                return true;
            }

            return false;
        }
    });
    return MyModal;
});

如何解决此问题?

那么,为什么要在渲染函数中创建
personModel
?问题是在建模过程中没有加载DOM元素setting@neelshah:完全正确…那么我如何修复它?您可以在渲染函数完成执行后加载模型