在车型私人样性质或Backbone.js的意见的 [英] Private-like properties in models or views of Backbone.js
问题描述
是否有可能有一个模型私有财产?就像在(构造函数)函数的局部声明的变量,不附这个
,但只能通过无论是在(构造)函数定义宣称本地和可见。
例如无BB查看:
Is it possible to have private properties in a model? Like the locally declared variables in a (constructor) function, not attached to this
, but declared locally and visible only by whatever is defined in the (constructor)function.
Example without BB View:
function MyView(aModel){
var $internalInput = $('<input>');
this.render: function($where){
$internalInput.val(aModel.get('SomeProperty'));
$where.append($('<div class="inputWraper">').append($internalInput));
};
this.toggleReadonly: function() {
toggle $internalInputs readonly attribute
}
...
+ Code to bind input.val to some aModel property(ies) and setup events
...
}
注意 internalInput
是外界无法访问和 AMODEL
也不可访问(通过MyView的至少) 。
所以,如果我想用Backbone.View执行上述MyView的,我如何做到这一点,并保持$ internalInput私人?
Note that internalInput
is not accessible to outside world and aModel
is also not accessible (through MyView at least).
So if I want to use Backbone.View to implement the above MyView, how would i do it and keep $internalInput 'private'?
推荐答案
您应该能够通过传递IIFE到来实现私有数据扩展定义你的主干对象时
,而不仅仅是一个普通的对象。例如:
You should be able to achieve private data by passing an IIFE to extend
when defining your Backbone objects, rather than just a plain object. For example:
var Thing = Backbone.Model.extend((function () {
var foo = "Private data!";
return {
bar: function () {
console.log(foo);
}
};
})());
这篇关于在车型私人样性质或Backbone.js的意见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!