簡體   English   中英

如何從外部文件訪問模型視圖內的變量

[英]How do I access to a variable inside a model view from an external file

嗨,我有一個與我的Backbone代碼有關的基本問題。

我首先在一個名為js.js的文件中初始化4 SpinnerView。 在名為app.js的主代碼文件中,我聲明了模型視圖,每個視圖中都有一個名為Spinner的模型。 在每個Spinner內部,都有一個稱為WordCollection的集合,並且在該集合內有稱為Word的模型。

問題是,如何僅在文件js.js的4個渲染之一(即第3個SpinnerView渲染)中訪問SpinnerView內部的“ test”變量。

所有幫助將不勝感激。 謝謝!

這是渲染Spinners的文件中的代碼示例:

//file js.js
(new SpinnerView()).render();
(new SpinnerView()).render();
(new SpinnerView()).render();
(new SpinnerView()).render();

這是我的主代碼文件中的代碼示例:

//file app.js
(function($) {

// model word
window.Word = Backbone.Model.extend({
    url: 'save.php',

     defaults: {
        word: '',
    }
});

//collection word
window.WordCollection = Backbone.Collection.extend({
    model: Word
});

// spinner model
window.Spinner = Backbone.Model.extend({

    url: '/beta/save.php',

    wordCollection: null,

    defaults: {
        title: 'title',
    },

    initialize: function() {
        this.wordCollection = new WordCollection();
    },

    addWord: function(bs) {
        this.wordCollection.add(bs);
    }

});

// spinner view
window.SpinnerView = Backbone.View.extend({
    template: null,
    spinner: null,
    el: '',
            test: false, //<---- THIS IS THE VARIABLE I WANT TO ACCESS

    initialize: function() {
        _.bindAll(this, 'focusAddWord', 'addWord', 'onEnterAddWord', 'focusSetTitle', 'setTitle', 'onEnterSetTitle');
        this.template = _.template($('#spinner-template').text());
        this.spinner = new Spinner();

    },

    render: function() {

        var el = $(this.template()).appendTo('.spinners');
        this.setElement(el);

    },

    focusAddWord: function() {

            this.$el.find('.add-word-input input').val('');
            this.$el.find('.add-word-input input').focus();

    },

    addWord: function() {
        var word = new Word();
        var val = this.$el.find('.add-word-input input').val();

        // validate minimum characters
        if(this.$el.find('.add-word-input input').val().length > 0){

            // go on
            this.spinner.addWord({
                word: val,
            });

            word.set({
                word: val,
            });

            word.toJSON();
            word.save();

            this.$el.find('.add-word-input').hide();
            this.renderWordCollection();
        }
        this.$el.find('.add-word-input').hide();

    },

    onEnterAddWord: function(ev) {
        if (ev.keyCode === 13) {
            this.$el.find('.add-word-input input').trigger('blur');
            this.$el.find('.viewbox').trigger('click');
        }
    },

    focusSetTitle: function() {
        this.$el.find('.set-title-input input').val('');
        this.$el.find('.set-title-input input').focus();
        this.$el.find('.set-title-input input').addClass('input-active');
    },

    setTitle: function() {

        var val = this.$el.find('.set-title-input input').val();

        if(this.$el.find('.set-title-input input').val().length > 0){

            // go on
            this.spinner.set('title', val);

            this.spinner.toJSON();
            this.spinner.save();

        }
    },

    onEnterSetTitle: function(ev) {
        if (ev.keyCode === 13) {
            this.$el.find('.set-title-input input').trigger('blur');
        }
    },

    // call after adding a word to spinner.
    renderWordCollection: function() {

        var wc = this.spinner.wordCollection; 
        var ListTemplate = _.template($('#word-collection-template').html(),{wc: wc});

        this.$el.find('ul').html(ListTemplate);

    }

});

})(jQuery);

尚不清楚您要對test做什么,但是要將其用作實例變量只需對其進行初始化:

window.SpinnerView = Backbone.View.extend({
     // code removed for brevity

        test: false, //<---- THIS IS THE VARIABLE I WANT TO ACCESS

    initialize: function() {
        _.bindAll(this, 'focusAddWord', 'addWord', 'onEnterAddWord', 'focusSetTitle', 'setTitle', 'onEnterSetTitle');
        this.template = _.template($('#spinner-template').text());
        this.spinner = new Spinner();
        this.test = false, //<---- PUT IT HERE
    },

然后,您可以從視圖內部的函數中訪問它:

focusAddWord: function() {
        console.log(this.test);
        this.$el.find('.add-word-input input').val('');
        this.$el.find('.add-word-input input').focus();

},

您還可以從外部訪問它:

var view = new SpinnerView();
view.render();
console.log(view.test);

並修改它:

view.test = true;

另外,不要忘了在實例化視圖時可以傳遞選項:

    initialize: function(options) {
        _.bindAll(this, 'focusAddWord', 'addWord', 'onEnterAddWord', 'focusSetTitle', 'setTitle', 'onEnterSetTitle');
        this.template = _.template($('#spinner-template').text());
        this.spinner = new Spinner();
        // use an empty `options` object if none is provided, fallback to `false` default
        this.test = (options || {}).mustBeTested || false,
    },

    // ...

    focusAddWord: function() {
      if(this.test){
        // do something when the view needs to be tested
      }
      this.$el.find('.add-word-input input').val('');
      this.$el.find('.add-word-input input').focus();
    },

然后,您可以根據需要簡單地傳遞選項:

(new SpinnerView()).render();
(new SpinnerView()).render();
(new SpinnerView({ mustBeTested: true })).render();
(new SpinnerView()).render();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM