Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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 在backbone.js中创建实时集合?_Javascript_Backbone.js_Backbone Views_Backbone.js Collections - Fatal编程技术网

Javascript 在backbone.js中创建实时集合?

Javascript 在backbone.js中创建实时集合?,javascript,backbone.js,backbone-views,backbone.js-collections,Javascript,Backbone.js,Backbone Views,Backbone.js Collections,我正在创建一个基本的主干应用程序,它应该支持多个用户,并且我希望在任何用户对集合进行更改时,我的集合视图能够为所有用户更新 我正在做以下工作: 我认为: /*Collection view - - - - - - -*/ App.Views.Contacts = Backbone.View.extend({ tagName: 'tbody', wrapper : $('#contacttable'), initialize: function() { /

我正在创建一个基本的主干应用程序,它应该支持多个用户,并且我希望在任何用户对集合进行更改时,我的集合视图能够为所有用户更新

我正在做以下工作:

我认为:

/*Collection view
- - - - - - -*/
App.Views.Contacts = Backbone.View.extend({
    tagName: 'tbody',
    wrapper : $('#contacttable'),
    initialize: function() {

        // custom events
        vent.on('hideparent', this.hideMe,this);
        vent.on('showparent', this.showMe,this);

        // standard events
        this.listenTo(this.collection, 'add', this.addOne);
        this.listenTo(this.collection, 'reset', this.addAll);
        this.listenTo(this.collection, 'all', this.render);

        // Load the collection data
        this.collection.fetch({reset:true});
    },
    render: function() {
        // publish view status
        vent.trigger();
        // append the list view to the DOM
        $('#contacttable').append(this.el);
        return this;
    },
    addAll : function() {
        this.collection.each( this.addOne, this);
    },
    addOne: function(model) {
        var contactView = new App.Views.Contact({ model: model});
        this.$el.append(contactView.render().el);
    },
    showMe: function() {
        this.wrapper.show();
    },
    hideMe: function() {
        this.wrapper.hide();
    }
});

/*Single view
- - - - - - - */
App.Views.Contact = Backbone.View.extend({
    tagName: 'tr',
    template: template('singlecontact'),
    events: {
        'click a.delete' : 'deleteContact'
    },
    initialize: function() {
        this.model.on('destroy', this.unrender, this); // doing this so item is only removed visually if its actually destroyed
        this.model.on('change', this.render, this);
    },
    render: function() {
        this.$el.html( this.template( this.model.toJSON() ) );
        return this;
    },
    deleteContact: function(e) {
        e.preventDefault();
        this.model.destroy();
    },
    unrender: function() {
        this.remove(); 
    }
});
在我的路由器中:

这适用于添加和编辑,但不适用于删除,这是一个问题1。它也感觉不太对

contactList: function() {
    vent.trigger('app:contacts');
    if (typeof App.AllContactsView == 'undefined') {
        App.AllContactsView = new App.Views.Contacts({ //init method runs now
            collection : App.contacts
        });

            // checking for updates
        function updateContacts() {
            App.AllContactsView.collection.fetch({merge:true });
        }

        setInterval(updateContacts, 1000);

    } else {
        App.AllContactsView.collection.fetch({add:true,remove:true});
    }

    ViewManagerTwo.showView(App.AllContactsView,true,false, false);
    return App.AllContactsView;
},

是否有一种本地方式——理想情况下比这种方式更好的方式来实时更新集合?

以下是有关使用websockets作为主干网传输层的一些信息,应该可以帮助您:


对于这类功能,您最好查看WebSocket。好的,很酷,我目前正在构建一个基本的应用程序,首先是为了学习BB,其次是为自己构建一个基本框架,我使用一个非常快速和肮脏的laravel后端,但接下来将讨论node express和sails,它们看起来非常支持套接字。如果你能在下面给我一个答案,并提供更多关于在前端使用BB插座的信息,我将不胜感激。想回答这个问题以便我关闭它吗?谢谢,堆栈溢出问题是一个很好的讨论,值得检查。