Backbone.js-使用jQuery append时出现问题

Backbone.js - trouble using jQuery append

本文关键字:问题 append js- 使用 jQuery Backbone      更新时间:2023-09-26

我在使用jQuery.append()和backknejs时遇到了一些问题。现在,当它尝试追加时,没有发生任何事情(除了它返回jQuery对象[在即时窗口中看到]),计数仍然为0。我尝试手动添加元素,但没有成功。我还尝试过只使用string()进行追加,出于某种原因,这是有效的。这里的想法是在页面加载时加载一些对象,并根据第一个框中选择的值在selectbox中更改其依赖项。但正如你所看到的,我有点被卡住了。(对于这样一件琐碎的事情来说,这很烦人)。我用作指南的来源如下:https://github.com/shinetech/CascadingSelectsWithBackboneJS/blob/master/public/javascripts/application.js

$(function () {
    window.AgfaRis = Backbone.View.extend({
        template: _.template($("#agfaris-template").html()),
        initialize: function () {
            _.bindAll(this, "render");
        },
        render: function () {
            $(this.el).html(this.template(this.model.toJSON()));
            this.EstablishHospitals();
            return this; // MUST return this
        },
// RELEVANT STUFF FROM HERE ON
        EstablishHospitals: function() {
            var hospitals = new Hospitals();
            hospitals.url = "/_Systemer/AgfaRis/GetHospitals/" + this.model.attributes.Id;
            var hospitalsView = new HospitalsView({el: $("#Hospitals"), collection: hospitals});
            var hospitalRolesView = new HospitalRolesView({el: $("#HospitalRoles"), collection: new HospitalRoles()});
            hospitalsView.hospitalRolesView = hospitalRolesView;
            hospitals.fetch();
        }
    });

    var Hospital = Backbone.Model.extend();
    var HospitalRole = Backbone.Model.extend();
    var Hospitals = Backbone.Collection.extend({ model: Hospital });
    var HospitalRoles = Backbone.Collection.extend({ model: HospitalRole });
    var LocationView = Backbone.View.extend({
        tagName: "option",
        initialize: function() {
            _.bindAll(this, "render");
        },
        render: function() {
             $(this.el).attr('value', this.model.get('Id')).html(this.model.get('Name'));
             return this;
        }
    });
    var LocationsView = Backbone.View.extend({
        events: {
            "change": "changeSelected"
        },
        initialize: function() {
            _.bindAll(this, 'addOne', 'addAll');
            this.collection.bind('reset', this.addAll);
        },
        addOne: function(location) {
            var locationView = new LocationView({ model: location });
            this.locationViews.push(locationView);
            $(this.el).append(locationView.render().el);
        },
        addAll: function() {
            _.each(this.locationViews, function(locationView) { locationView.remove(); });
            this.locationViews = [];
            this.collection.each(this.addOne);
            if(this.selectedId) {
                $(this.el).val(this.selectedId);
            }
        },
        changeSelected: function() {
            this.setSelectedId($(this.el).val());
        },
        populateFrom: function(url) {
            this.collection.url = url;
            this.collection.fetch();
            this.setDisabled(false);
        },
        setDisabled: function(disabled) {
            $(this.el).attr('disabled', disabled);
        }
    });
    var HospitalsView = LocationsView.extend({
        setSelectedId: function(hospitalId) {
            this.hospitalRolesView.selectedId = null;
            this.hospitalRolesView.setHospitalId(hospitalId);
        }
    });
    var HospitalRolesView = LocationsView.extend({
        setHospitalId: function(hospitalId) {
            this.populateFrom("/_Systemer/AgfaRis/GetHospitalRoles/" + hospitalId);
        }
    });
});

调试数据

this.model.get('Id') and .get('Name') works, they return the value and text seen below.
(HtmlOptionElement)
this.locationViews[0].render().el
{...}
    defaultSelected: false
    form: null
    index: 0
    label: ""
    selected: false
    text: "--- Velg Sykehus ---"
    value: "--- Velg Sykehus ---"
    dataFld: ""
    dataFormatAs: ""
    dataSrc: ""
    currentStyle: {...}
    runtimeStyle: {...}
    accessKey: ""
    className: ""
    contentEditable: "inherit"
    dir: ""
    disabled: false
    id: ""
    innerHTML: "--- Velg Sykehus ---"
    isContentEditable: false
    lang: ""
    offsetHeight: 0
    offsetLeft: 0
    offsetParent: null
    offsetTop: 0
    offsetWidth: 0
    onabort: null
    onblur: null
    oncanplay: null
    oncanplaythrough: null
    onchange: null
    onclick: null
    oncontextmenu: null
    ondblclick: null
    ondrag: null
    ondragend: null
    ondragenter: null
    ondragleave: null
    ondragover: null
    ondragstart: null
    ondrop: null
    ondurationchange: null
    onemptied: null
    onended: null
    onerror: null
    onfocus: null
    oninput: null
    onkeydown: null
    onkeypress: null
    onkeyup: null
    onload: null
    onloadeddata: null
    onloadedmetadata: null
    onloadstart: null
    onmousedown: null
    onmousemove: null
    onmouseout: null
    onmouseover: null
    onmouseup: null
    onmousewheel: null
    onpause: null
    onplay: null
    onplaying: null
    onprogress: null
    onratechange: null
    onreadystatechange: null
    onreset: null
    onscroll: null
    onseeked: null
    onseeking: null
    onselect: null
    onstalled: null
    onsubmit: null
    onsuspend: null
    ontimeupdate: null
    onvolumechange: null
    onwaiting: null
    outerHTML: "<option value="--- Velg Sykehus ---">--- Velg Sykehus ---</option>"
    style: {...}
    tabIndex: 0
    title: ""
    all: {...}
    behaviorUrns: {...}
    canHaveChildren: true
    canHaveHTML: true
    children: {...}
    document: {...}
    filters: {...}
    hideFocus: false
    innerText: "--- Velg Sykehus ---"
    isDisabled: false
    isMultiLine: true
    isTextEdit: false
    language: ""
    onactivate: null
    onafterupdate: null
    onbeforeactivate: null
    onbeforecopy: null
    onbeforecut: null
    onbeforedeactivate: null
    < More... (The first 100 of 259 items were displayed.) >

<select id="Hospitals">
    <option value="">Select</option>
</select><br />
<select id="HospitalRoles">
    <option value="">Select</option>
</select>

是的,将"OPTIONS"元素附加到SELECT元素总会有问题。如果您删除VIEW LocationView并使用它进行添加.el.options.add,那么您将不会有问题。。。

http://www.w3schools.com/jsref/coll_select_options.asp