gpt4 book ai didi

javascript - 如何从外部文件访问模型 View 内的变量

转载 作者:行者123 更新时间:2023-11-28 01:37:02 25 4
gpt4 key购买 nike

您好,我有一个与我的 Backbone 代码相关的基本问题。

我首先在名为 js.js 的文件中初始化 4 SpinnerView。在名为 app.js 的主代码文件中,我声明了模型 View ,并且每个 View 内都有一个名为 Spinner 的模型。每个 Spinner 内部都有一个名为 WordCollection 的集合,该集合内有名为 Word 的模型。

问题是,如何访问 SpinnerView 内的“test”变量,仅在 js.js 文件中的 4 个渲染之一(即第 3 个 SpinnerView 渲染)中。

所有帮助将不胜感激。谢谢!

以下是我渲染 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
},

然后,您可以从 View 内的函数内访问它:

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;

此外,不要忘记在实例化 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();
},

然后您只需传递适当的选项即可:

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

关于javascript - 如何从外部文件访问模型 View 内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com