2014-10-08 179 views
0

注意:我是一個非常非常開始在骨幹平臺! :)Backbone.js模型緩存

我有這個型號的骨幹:

var errors_model = Backbone.Model.extend(
{ 
     total_errors  : 0, 
     error_messages : {}, 
     initialize  : function() 
     { 
      this.set('total_errors',  0); 
      this.set('error_messages',  {}); 
     }, 
     add_error  : function (tab, field, error) 
     { 
      var error_messages = this.get('error_messages'); 
      var error_md5 = this.md5(error); 

      if(!(tab in error_messages)) 
      { 
       error_messages[tab] = {}; 
      } 

      if(!(field in error_messages[tab])) 
      { 
       error_messages[tab][field] = {}; 
      } 

      if(!(error_md5 in error_messages[tab][field])) 
      { 
       error_messages[tab][field][error_md5] = error; 
      } 

      this.set('error_messages', error_messages); 
      this.increase_errors(); 
      console.log(error_messages); 
     }, 
     remove_error  : function (tab, field, error) 
     { 
      var error_messages = this.get('error_messages'); 
      var error_md5  = this.md5(error); 

      if(!(tab in error_messages) && !(field in error_messages[tab]) && !(error_md5 in error_messages[tab][field])) 
      { 
       return; 
      } 

      delete error_messages[tab][field][error_md5]; 

      this.set('error_messages', error_messages); 
      this.decrease_errors(); 
      console.log(error_messages); 
     }, 
     increase_errors : function() 
     { 
      var total_errors = this.get('total_errors'); 

      if('undefined' === typeof(total_errors)) 
      { 
       total_errors = 0; 
      } 

      ++total_errors; 

      this.set('total_errors', total_errors); 
     }, 
     decrease_errors : function() 
     { 
      var total_errors = this.get('total_errors'); 

      if('undefined' === typeof(total_errors)) 
      { 
       total_errors = 0; 
      } 

      if(0 === total_errors) 
      { 
       return; 
      } 

      --total_errors; 

      this.set('total_errors', total_errors); 
     } 
    } 
); 

com      = window.com || {}; 
com.wp      = com.wp  || {}; 
com.wp.srb     = com.wp.srb || {}; 
com.wp.srb.errors_model = new errors_model(); 

,然後我用這個代碼:

com.wp.srb.errors_model.add_error('#ad',  '#image_upload', 'This is an error'); 
com.wp.srb.errors_model.add_error('#ad',  '#image_upload', 'Another error'); 
com.wp.srb.errors_model.add_error('#ad',  '#image_upload', 'Yet another error'); 
com.wp.srb.errors_model.add_error('#ad',  '#image_url',  'URL error'); 

setTimeout(
    function() 
    { 
     com.wp.srb.errors_model.remove_error('#ad', '#image_upload', 'Another error'); 
    }, 
    3500 
); 

但我得到的結果是這樣兩個動作(com.wp.srb.errors_model.add_errorcom.wp.srb.errors_model.remove_error) :

Object {#ad: Object} 
    #ad: Object 
     #image_upload: Object 
      13b7afb8b11644e17569bd2efb571b10: "This is an error" 
      69553926a7783c27f7c18eff55cbd429: "Yet another error" 
     #image_url: Object 
      2b4a9847e26368312704b8849de9247a: "URL error" 
      __proto__: Object 
     __proto__: Object 
    __proto__: Object 

Object {#ad: Object} 
    #ad: Object 
     #image_upload: Object 
      13b7afb8b11644e17569bd2efb571b10: "This is an error" 
      69553926a7783c27f7c18eff55cbd429: "Yet another error" 
     #image_url: Object 
      2b4a9847e26368312704b8849de9247a: "URL error" 
      __proto__: Object 
     __proto__: Object 
    __proto__: Object 

Object {#ad: Object} 
    #ad: Object 
     #image_upload: Object 
      13b7afb8b11644e17569bd2efb571b10: "This is an error" 
      69553926a7783c27f7c18eff55cbd429: "Yet another error" 
     #image_url: Object 
      2b4a9847e26368312704b8849de9247a: "URL error" 
      __proto__: Object 
     __proto__: Object 
    __proto__: Object 

Object {#ad: Object} 
    #ad: Object 
     #image_upload: Object 
      13b7afb8b11644e17569bd2efb571b10: "This is an error" 
      69553926a7783c27f7c18eff55cbd429: "Yet another error" 
     #image_url: Object 
      2b4a9847e26368312704b8849de9247a: "URL error" 
      __proto__: Object 
     __proto__: Object 
    __proto__: Object 

Object {#ad: Object} 
    #ad: Object 
     #image_upload: Object 
      13b7afb8b11644e17569bd2efb571b10: "This is an error" 
      69553926a7783c27f7c18eff55cbd429: "Yet another error" 
     #image_url: Object 
      2b4a9847e26368312704b8849de9247a: "URL error" 
      __proto__: Object 
     __proto__: Object 
    __proto__: Object 

所以問題是,骨幹模型,someho w緩存對象屬性?我做錯了什麼?

注意:這是插件的一部分,將用於WordPress儀表板,我不知道WordPress是否已經使用任何插件來緩存模型數據。

+0

所以問題是,當你添加新的數據到模型,它似乎已經被添加過? – STT 2014-10-08 10:07:46

+0

是的,沒錯。總的來說,我認爲這不應該發生。 – 2014-10-08 10:08:31

+0

當您僅調用單一的add_error調用時,這是否也會發生?這裏還有關於其他錯誤的數據嗎? – STT 2014-10-08 10:10:58

回答

1

控制檯持有引用的對象,所以當你的代碼執行後,檢查它們,對象的狀態將是因爲它是現在,不是因爲它是當你調用:

console.log(error_messages); 

這可能適用於字符串或數字,但不適用於對象。值得慶幸的是,你可以使用把一個對象轉換成字符串:

JSON.stringify(error_messages); 

,這將給你的對象的快照在執行的時間。

+0

感謝您的回覆! :) – 2014-10-08 10:31:37