如何从外部文件访问模型视图中的变量

时间:2014-01-29 08:35:58

标签: javascript backbone.js

您好我有一个与我的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);

1 个答案:

答案 0 :(得分:1)

您想要对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();