2013-07-31 211 views
1

我试图重写得到:在合金模型调用,非常相似的骨干,我写这可是不行的覆盖get方法

extendModel: function(Model) {  
     _.extend(Model.prototype, { 
      // extended functions and properties go here 
      get: function (attr) { 
       if (attr=='image') 
       { 
        return Ti.Utils.base64decode(this['image']) 
       } 

        return this[attr]; 
       } 
     }); 

     return Model; 
    }, 
+0

你在控制台上得到任何错误信息?你在Chrome控制台中调试这段代码吗?我正在寻找的 – Sergey

回答

2

下面是我正在重写集并添加方法希望它可以帮助你:

exports.definition = { 
    config: { 

     adapter: { 
      type: "properties", 
      collection_name: "careCenter", 
      idAttribute : "CareCenterID" 
     } 
    },  
    extendModel: function(Model) {  
     _.extend(Model.prototype, { 
      idAttribute : "CareCenterID" 
      // extended functions and properties go here 
     }); 

     return Model; 
    }, 
    extendCollection: function(Collection) {   
     _.extend(Collection.prototype, { 
      add : function(attrs, opts){ 
       var isDuplicated = false; 
       if(attrs && attrs.get){ 
        isDuplicated = this.any(function(model){ 
         return model.get("CareCenterID") === attrs.get("CareCenterID"); 
        }); 
       } 
       if(isDuplicated){ 
        return false; 
       } else { 
        Backbone.Collection.prototype.add.call(this, attrs, opts); 
       } 
      }, 
      comparator : function(model){ 
       return -model.get("state"); 
      } 
     }); 

     return Collection; 
    } 
} 


extendModel: function(Model) {  
     _.extend(Model.prototype, { 
      idAttribute : "RecipientID", 
      set : function(attrs, opts){ 
       if(attrs.Age != null){ 
        var age = attrs.Age; 
        var result = ""; 
        if(age <= Alloy.CFG.INFANT){ 
         result = "infant"; 
        } else if(age <= Alloy.CFG.CHILD){ 
         result = "child"; 
        } else if(age <= Alloy.CFG.TEENAGER){ 
         result = "teenager"; 
        } else { 
         result = "adult"; 
        } 
        attrs.Group = result; 
       } 

       return Backbone.Model.prototype.set.call(this, attrs, opts); 
      } 
     }); 

     return Model; 
    }, 
+0

非常感谢你! – sdalpos