首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Backbone.js子视图模板不呈现,尽管呈现函数确实运行

Backbone.js子视图模板不呈现,尽管呈现函数确实运行
EN

Stack Overflow用户
提问于 2013-01-01 09:10:00
回答 1查看 379关注 0票数 0

不幸的是,我已经为我的项目写了一个子视图。虽然它以前呈现得很好,而且是我想要的那样,但我知道我的应用程序结构不正确。我正处于重组的过程中,其他的一切都很有魅力,但是我的新设置似乎没有在el中呈现出来。el呈现得非常好,但没有插入任何内容。我在其中放置了一个console.log(),它打印了本应呈现的template,它是完美无缺的。但是,that.$.el.html()行根本不呈现模板。我不知道这是怎么回事。文件在下面。我的下流-我要死了!

contactlist.js (视图):

代码语言:javascript
复制
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 (视图):

代码语言:javascript
复制
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;

});
EN

回答 1

Stack Overflow用户

发布于 2013-01-01 13:41:46

在绑定el元素时检查它是否可用。好像还没呈现出来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14109205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档