在Backbone Relational中如何在fetch()上静默添加:*事件

时间:2013-01-25 19:04:24

标签: backbone.js backbone-relational

当我使用.fetch()骨干加载我的模型时Backbone Relational会为它添加的每个问题触发一个add:survey_question(这与通过我的survey_questions集合加载某些内容时会发生的情况相同.add()。更糟糕的是,add:survey_questions不尊重我的survey_questions集合上的comparator

我想要一种方法来沉默添加:survey_questions发生

收听调查模型(SurveyBuilder)和问题模型(嵌入SurveyQuestion模型中)内的所有事件时的Console.log输出

//START of .fetch() on the Survey Model
SurveyBuilder 
["relational:change:survey_questions", child, Array[3], Object]
 app.js:25338
SurveyBuilder 
["add:survey_questions", child, child, Object]
 app.js:25338
Question 
["add:survey_question", child, Backbone.Collection, Object]
 app.js:25575
SurveyBuilder 
["add:survey_questions", child, child, Object]
 app.js:25338
Question 
["add:survey_question", child, Backbone.Collection, Object]
 app.js:25575
SurveyBuilder 
["add:survey_questions", child, child, Object]
 app.js:25338
Question 
["add:survey_question", child, Backbone.Collection, Object]
 app.js:25575
SurveyBuilder 
["sync", child, Object, Object]
 app.js:25338

//START OF Manual .add()
SurveyBuilder 
["add:survey_questions", child, child, Object]
 app.js:25338
SurveyQuestion 
["change:question_number", child, 4, undefined]
 app.js:25578
SurveyQuestion 
["change", child, undefined]
 app.js:25578

这是我的模特:

m.Question = m.BaseModel.extend({
    urlRoot: '/api/questions',

    initialize: function(){
        if(this['type']) {
            this.set('type', this['type']);
        } else if(this.get('type')) {
            this.type = this.get('type');
        }

        m.BaseModel.prototype.initialize.apply(this, arguments);
    },

    defaults: {
        question_text: '',
        question_number: 1,
        comment_field: true
    },
    calculateProperties: function(){
        return {
            'question_id': this.getID()
        };
    },
    subModelTypes: {
        'Numeric': 'APP.Models.NumericQuestion',
        'MultipleChoice': 'APP.Models.MultipleChoiceQuestion',
        'Checkboxes': 'APP.Models.CheckboxesQuestion',
        'FreeText': 'APP.Models.FreeTextQuestion',
        'TimePeriod': 'APP.Models.TimePeriodQuestions',
        'Sorting': 'APP.Models.SortingQuestion',
        'Grading': 'APP.Models.GradingQuestion',
        'Slider': 'APP.Models.SliderQuestion',
        'AgreeDisagree': 'APP.Models.AgreeDisagreeQuestion',
        'YesNo': 'APP.Models.YesNoQuestion'
    }
});

m.SurveyQuestion = m.BaseModel.extend({
    defaults: {
        'question_number': 1
    },
    relations: [{
        type: Backbone.HasOne,
        key: 'question',
        relatedModel: 'APP.Models.Question',
        includeInJSON: '_id',
        reverseRelation: {
            type: Backbone.HasMany,
            key: 'survey_question',
            includeInJSON: false
        }
    }]
});

m.Survey = m.BaseModel.extend({
    urlRoot: '/api/surveys',
    defaults: {
        start_date: moment().format('YYYY-MM-DD'),
        end_date: moment().add('weeks', 3).format('YYYY-MM-DD'),
        title: moment().format('MMMM') + ' Economist Survey'
    },
    relations: [{
        type: Backbone.HasMany,
        key: 'survey_questions',
        relatedModel: 'APP.Models.SurveyQuestion',
        collectionType: 'APP.Collections.SurveyQuestions',
        includeInJSON: true,
        reverseRelation: {
            key: 'survey',
            includeInJSON: true
        }
    }]
});

0 个答案:

没有答案