2012-01-19 35 views
6

是否可以在模型中擁有私有屬性?像(構造函數)函數中的局部聲明變量一樣,不附加到this,但是在本地聲明並且只能由(構造函數)函數中定義的任何東西可見。 例無BB查看:Backbone.js模型或視圖中類似私人的屬性

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'私人'?

+1

注意「私有」實現的唯一一件事就是讓代碼變得更慢。 – Raynos

+0

請你詳細說明一下嗎?我只是希望它用於信息隱藏的目的,所以我可以在將來改變它,而不用擔心有人可能直接使用它而依賴它。但如果這意味着減慢代碼,那麼這是一個問題。我在哪裏可以找到有關您的索賠的更多信息? – Paralife

+1

[閉包有大量開銷](http://stackoverflow.com/a/8729939/419970),[私人狀態很貴但可以做得更好](http://raynos.org/blog/23/Prototypes-and - 私人狀態)。我個人的建議是對'_foo'使用可能會改變的_internal_屬性。另外,如果有人依賴於_internal_屬性,當你的API發生變化時,它就是_their_問題,而不是你的問題 – Raynos

回答

10

在定義Backbone對象時,您應該能夠通過將IIFE傳遞給extend來實現私有數據,而不僅僅是一個普通對象。例如:

var Thing = Backbone.Model.extend((function() { 
    var foo = "Private data!"; 

    return { 
    bar: function() { 
     console.log(foo); 
    } 
    }; 
})()); 
+10

這會給你更類似於私有靜態屬性的東西。如果你想模擬私有實例屬性(比如OP的var-in-constructor技術),那麼你會被共享狀態引發,這會讓你不知所措。 – matthewwithanm

+0

毫無疑問,它會起作用,但是實現骨幹中私有變量的唯一/更好的方法?這個答案講的是不同的方法http://stackoverflow.com/a/16320901/2253756 – KShirish

2

你最好了與

var Thing = Backbone.Model.extend(
    { 
     constructor : function() 
     { 
      var _value = "Private data!"; 

      this.getValue = function() 
      { 
       return _value; 
      }; 
      this.setValue = function (value) 
      { 
       _value = value; 
      }; 
     } 
    }); 
+0

所以它不同使用'initialize'和'構造函數'? –

+0

是的,請參考這裏的答案:https://stackoverflow.com/questions/10118988/whats-the-difference-between-initialize-and-constructor-on-a-backbone-model –

0

JavaScript是樂趣!

var Thing = (function() { 
    var number_of_things = 0; 

    return function (options) { 
     var value = "Private data!"; 

     return new (Backbone.Model.extend({ 
      constructor: function constructor() { 
       number_of_things += 1; 
      }, 

      getValue: function getValue() { 
       return value; 
      } 
     }))(); 
    }; 
}()); 

我對這個「東西」的每個實例也是OOP術語中的一個子類有點擔心。

0

在使用Broserify.js與骨幹網的情況下(與任何真正的中等以上的項目),我發現下面的辦法有私人VAR和功能:

myView.js

'use strict'; 

var config  = require('../config.js'), 

    private_var = 'private variable', 
    my_private_fn = function() { 
     ... 
    }; 


module.exports = Backbone.Model.extend({ 
    initialize: function() { 
     this.my_public = 'public variable'); 

     console.log('This is my' + this.my_public); 
     console.log('This is my' + my_private); 
    }, 
}); 

採取的想法這裏去與Browserify:P

0

最簡單的方法是:

... 
initialize:function(properites){    
    // Init the logic with private and public methods/variable 
    this.logic.initFirst(this); 
    // Use public methods 
    this.logic.doSomething(); 
}, 

logic:{   
    initFirst:function(modelOrView){ 
     // Do not continue if already initiated 
     if(this.instance !== undefined) return; 

     // Write all logic here 
     this.instance = (function(logic, modelOrView){    
      // Private variables 
      var private = "private";     

      // Public methods 
      logic.doSomething = function(){ 
       console.log(private, modelOrView); 
      }; 

      // Private methods 
      function hidden(){ 

      } 

     }(this, modelOrView)); 
    } 

},