0

不幸的是,我为我的项目写了一个子视图。虽然它之前渲染得非常好并且完全符合我的要求,但我了解到我的应用程序结构不正确。我正在重组,其他一切都像一个魅力,但我的新设置似乎没有在它的el. el渲染非常好,但没有插入任何内容。我console.log()在打印的template那个应该渲染的地方放了一个,它是完美的。但是,该that.$.el.html()行根本不呈现模板。我不确定发生了什么。该文件如下。我的野心正在杀死我!

contactlist.js(查看):

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/contactlist.html',
  'collections/contacts'
], function($, _, Backbone, contactListTemplate, Contacts){

  var ContactListView = Backbone.View.extend({
    initialize: function () {
        var that = this;
        this.options = {
            idclicked: null,
            query: null,
            scrolled: 0
        };
        this.options.get = function (property) {
            return that.options[property];
        };
        this.options.set = function (properties) {
            for (property in properties) {
                that.options[property] = properties[property];
            }
        };
    },
    el: '.contact-list',
    render: function() {
        var that = this;
        var contacts = new Contacts();
        contacts.fetch({
            success: function(contacts) {
                var results = contacts.models;
                if (that.options.query || that.options.query === '') {
                    var query = that.options.query.toUpperCase();
                    var results = contacts.toArray();
                        results = contacts.filter(function(contact){
                            return _.any(['firstname', 'lastname', 'email', 'phonenumber'], function(attr){
                                return contact.get(attr).toString().toUpperCase().indexOf(query) !== -1;
                            });
                        });
                        that.options.set({idclicked: null});
                }
                if (!that.options.idclicked && results[0]) {
                    that.options.idclicked = results[0].get('id');
                    Backbone.history.navigate('/contacts/' + results[0].get('id'), {trigger: true});
                }
                var template = _.template(contactListTemplate, {contacts: results, idclicked: that.options.idclicked});
                that.$el.html(template);
                $(document).ready(function() {
                    $('.contact-list').scrollTop(that.options.scrolled);
                });
            }
        });
    },
    events: {
        'click .contact': 'clickContact'
    },
    clickContact: function (ev) {
        $('.contact-clicked').removeClass('contact-clicked').addClass('contact');
        $(ev.currentTarget).addClass('contact-clicked').removeClass('contact');
        window.location.replace($(ev.currentTarget).children('a').attr('href'));
    }
  });

  return ContactListView;

});

更新:

尝试了一件有效的事情,但不确定这是否是一种好习惯。下面的代码是父视图:

allcontacts.js(查看):

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/allcontacts.html',
  'views/contactlist',
  'collections/contacts'
], function($, _, Backbone, allContactsTemplate, ContactListView, Contacts) {
  var AllContactsView = Backbone.View.extend({
    initialize: function () {
        var that = this;
        this.options = {
            idclicked: null,
            pageloaded: false,
            renderlist: true,
            scrolled: null
        };
        this.options.get = function (property) {
            return that.options[property];
        };
        this.options.set = function (properties) {
            for (property in properties) {
                that.options[property] = properties[property];
            }
        };

        that.contactListView = new ContactListView();
    },
        el: '.allcontacts',
        render: function() {
            var that = this;
            if (!that.options.pageloaded) {
                var template = _.template(allContactsTemplate, {});
                that.$el.html(template)
        }
        if (that.options.renderlist) {
                this.contactListView.options.set({idclicked: that.options.idclicked, scrolled: that.options.scrolled});
                this.contactListView.setElement('.contact-list').render();
            }
        },
        events: {
        'keyup .search': 'search',
        'submit .search-contacts-form': 'cancelSubmit'
        },
        search: function (ev) {
            this.contactListView.options.set({idclicked: this.options.idclicked, query: $(ev.currentTarget).val(), scrolled: this.options.scrolled});
            this.contactListView.setElement('.contact-list').render();
        },
        cancelSubmit: function (ev) {
            return false;
        }
    });

  return AllContactsView;

});
4

1 回答 1

0

绑定时检查您的el元素是否可用。好像还没有渲染。

于 2013-01-01T13:41:46.257 回答