简体   繁体   English

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

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

Hi I have a basic question related to my Backbone code. 嗨,我有一个与我的Backbone代码有关的基本问题。

I first initialize 4 SpinnerView in a file called js.js. 我首先在一个名为js.js的文件中初始化4 SpinnerView。 In my main code file called app.js I declare the model views and inside each view there is a model called Spinner. 在名为app.js的主代码文件中,我声明了模型视图,每个视图中都有一个名为Spinner的模型。 Inside each Spinner there is a collection called WordCollection and inside the collection there are models called Word. 在每个Spinner内部,都有一个称为WordCollection的集合,并且在该集合内有称为Word的模型。

The question is, how do I access to "test" variable inside SpinnerView, only in one of the 4 renders (ie the 3rd SpinnerView render) from the file js.js. 问题是,如何仅在文件js.js的4个渲染之一(即第3个SpinnerView渲染)中访问SpinnerView内部的“ test”变量。

All help will be appreciated. 所有帮助将不胜感激。 Thanks! 谢谢!

Here is a sample of my code in the file where I render the Spinners: 这是渲染Spinners的文件中的代码示例:

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

And here is a sample of my code from my main code file: 这是我的主代码文件中的代码示例:

//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);

It's not entirely clear what you want to do with test , but to use it as an instance variable simply initialize it: 尚不清楚您要对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
    },

Then, you can access it from within function inside the view: 然后,您可以从视图内部的函数中访问它:

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

},

And you can also access it from outside: 您还可以从外部访问它:

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

And modify it: 并修改它:

view.test = true;

In addition, don't forget you can pass options when instanciating a view: 另外,不要忘了在实例化视图时可以传递选项:

    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();
    },

You then simply pass options as appropriate: 然后,您可以根据需要简单地传递选项:

(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