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;
        }
    }
}))()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚俊飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值