简体   繁体   English

检查主干是否已获取或更改?

[英]Check if backbone has been fetched, or changed?

I need to have two url properties inside my Backbone.Collection.extend() because if a collection is fetched then I need to use a specific url if the collection gets a new model then I want to change the url 我需要在Backbone.Collection.extend()包含两个url属性,因为如果fetched了一个集合,那么如果该集合获得了一个新模型,那么我需要使用一个特定的url ,那么我想更改该url

module.exports = MessagesCollection = Backbone.Collection.extend({
    initialize: function(models, options) {
        this.id = options.id;
    },
    url: function() {
        if (fetch method is called) {
            return '/api/messages/' + this.id;
        } else {
            // here if a model is being added?
            return '/api/messages'
        }
    },
    model:  MessageModel
});

The reason for this is because I only want to pull down the models from the server based on the user. 这样做的原因是因为我只想根据用户从服务器下拉模型。

var me = new MeModel();    
me.fetch({
    success: function(response) { 
        App.data.me = me;
        var messages = new MessagesCollection([], { id: response.get('user_id') });
        messages.fetch({
            success: function() {
                App.data.messages = messages;
                App.core.vent.trigger('app:start');
            } 
        }); 

    } 
});

When the user creates a new model within the app I want it to go into the main collection? 当用户在应用程序中创建新模型时,我希望它进入主集合吗?

Does this mean I should create a sub collection based on the main collection somehow? 这是否意味着我应该以某种方式基于主集合创建一个子集合?

Edit: 编辑:

My create looks like this somewhere else in the app window.App.data.messages.create(Message); 我的创建在应用程序window.App.data.messages.create(Message);其他地方看起来像这样window.App.data.messages.create(Message); I am thinking maybe I could write something like 我在想也许我可以写一些像

    var me = new MeModel();    
    me.fetch({
        success: function(response) { 
            App.data.me = me;
            var messages = new MessagesCollection([], { id: response.get('user_id') });
            var allMessages = new MessagesCollection();
            messages.fetch({
                success: function() {
                    App.data.messages = messages;
                    App.data.allMessages = allMessages;
                    App.core.vent.trigger('app:start');
                } 
            }); 

        } 
    });

Then create window.App.data.allMessages.create(Message); 然后创建window.App.data.allMessages.create(Message); > It sounds like it can cause problems IDK any ideas? >听起来IDK可能会引起问题吗?

Edit: 编辑:

The above worked but I had to create a new Backbone.Collection.extend() passing the same model but just writing it like 上面的工作,但我不得不创建一个新的Backbone.Collection.extend()传递相同的模型,但只是像

var Backbone = require('backbone'),
    MessageModel = require('../models/message');

module.exports = AllMessagesCollection = Backbone.Collection.extend({
    model:  MessageModel,
    url: '/api/messages'
});

So let me really break this question down, is this solution problematic. 因此,让我真正分解这个问题,这个解决方案是否有问题。 What is the best way to do this? 做这个的最好方式是什么? The worst thing I can think of is bandwidth, using this method I would constantly be sending requests! 我能想到的最糟糕的事情是带宽,使用这种方法,我将不断发送请求!

If you need to use different url only when create new model you can override collection.create method: 如果仅在创建新模型时需要使用其他url,则可以覆盖collection.create方法:

var MessagesCollection = Backbone.Collection.extend({
    initialize: function(models, options) {
        this.id = options.id;
    },
    url: function() {
      return '/api/messages/' + this.id;
    },
    create: function(model, options){
      var extendedOptions = _.extend(options || {}, {url: '/api/messages'});
      return this.constructor.__super__.create.call(this, model, extendedOptions);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM