appcan MVVM 中使用computeds对model属性进行扩展和处理

由于我们从接口获取的数据格式JSON格式,很多标记都是boolean或者int,如何把这些标记转换为页面显示的文字描述,或者通过几个属性运算得到一个新的输出结果,这就要用到model的computeds方法,直接举个栗子吧:

var Model = new(MVVM.Model.extend({
    defaults:{ 
        orderList:[{
         title:'', 
         pay_price:'',  
        }],  
        realname:'',
        address:'',  
        ordersn:'', 
        time_format:'',  
        mobile:'', 
    },
    initialize: function() {
        return;
    },
    parse: function(data) {
        //console.log(data);   
        return data;
    },
    validate: function(attrs, options) {
        return ;            
    },
    computeds: {//从这里开始
        title:{
          get:function(){
              var orderList = this.get('orderList'); 
              return orderList[0].title;
          }  
        },
        pay_price:{
            get:function(){
              var orderList = this.get('orderList'); 
              return orderList[0].pay_price;
            }
        },
        show_expresscom:{
            get:function(){
                if(this.get('expresscom') == ''){
                    return "暂无";
                }else{
                    return this.get('expresscom'); 
                }
            }
            
        },
        show_expresssn:{
            get:function(){
                if(this.get('expresssn') == ''){
                    return "暂无";
                }else{
                    return this.get('expresssn'); 
                }
            }            
        }, 
        show_status:{//订单状态处理
            get:function(){
                var str_status = "未知状态";
                var int_stat = 100;
                if(this.get('orderList') != []){                    
                    var orderList = this.get('orderList');                    
                    for (var i=0; i < orderList.length; i++) {
                      int_stat = parseInt(orderList[i].status);
                      break;
                    };
                }
               switch(int_stat){
                   case 0:
                       str_status = "待支付";
                       break;
                   case 1:
                       str_status = "待发货";
                       break;
                   case 2:
                       str_status = "待收货";
                       break;
                   case 3:
                       str_status = "已完成";
                       break;
                   case -1:
                       str_status = "已关闭";
                       break;
                   case -6:
                       str_status = "已退款";
                       break;
                   case -5:
                       str_status = "已退货";
                       break;
                   case -4:
                       if(this.get('return_status') == 1){
                            str_status = "验货中";
                       }else{
                           str_status = "退货中";
                       }                      
                       break;
                    case -5:
                       str_status = "已退货";
                       break;
                   default:
                       break;
               }
               return  str_status; 
             }           
        },         
    },
    sync: function(method, model, options) {
        switch (method) {
        case "create":

            break;
        case "update":

            break;
        case "patch":

            break;
        case "read":            
            Service.request({},options);
            break;
        case "delete":

            break;
        default:
            break;
        }
    }
}))()


你可能感兴趣的:(appcan MVVM 中使用computeds对model属性进行扩展和处理)