ViewModel绑定记录幻影 [英] ViewModel bind record phantom

查看:156
本文介绍了ViewModel绑定记录幻影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想隐藏一个复选框,取决于记录是幻影。试图使用viewmodels来实现它,但是似乎不起作用。
相关代码见下文。为了简洁,我已经省略了无关的代码。
viewModel与视图的绑定按预期工作。当我尝试绑定 activeRecord.name 到标题属性2路数据绑定正常工作。



Viewmodel

  Ext.define('MyApp.view.content.ContentViewModel',{
扩展:'Ext.app.ViewModel',
alias:'viewmodel.content',

data:{
activeRecord:null
}
} );

控制器

  var contentWindow = Ext.widget('content-details'); 
contentWindow.getViewModel()。set('activeRecord',contentBlock);

查看

  viewmodel:'content',
items:[
{
xtype:'checkbox',
boxLabel:'我的复选框'
bind:{
hidden:'{!activeRecord.phantom}'
}
}
]


解决方案

我们最终使用了一个Model的以下基类,它比ViewModel更方便而不是一个公式。 / p>

  //使用你自己的名字而不是BaseModel 

Ext.define ('BaseModel',{
extends:'Ext.data.Model',

fields:[{
name:'phantom',
persist:false,
convert:function(v,rec){
var id = rec.data [rec.idProperty];
return!id ||(Ext.isString(id)&& .indexOf(rec.entityName)== 0);
}
}],

commit:function(silent,modifiedFieldNames){
this.data.phantom = false;
this.callParent(arguments);
}
});

然后你可以使用你想要的绑定

  bind:{
hidden:'{!activeRecord.phantom}'
}
pre>

I want to hide a checkbox depending on wheter a record is phantom. Trying to implement this using viewmodels, but it doesn't seem to work. See below for the related code. I've left out unrelated code for brevity. The binding of the viewModel to the view is working as expected. When I try to bind activeRecord.name to the title attribute 2-way data binding is working correctly.

Viewmodel

Ext.define('MyApp.view.content.ContentViewModel', {
    extend: 'Ext.app.ViewModel',
    alias: 'viewmodel.content',

    data: {
        activeRecord: null
    }
});

Controller

var contentWindow = Ext.widget('content-details');
contentWindow.getViewModel().set('activeRecord', contentBlock);

View

viewmodel: 'content',
items: [
    {
        xtype: 'checkbox',
        boxLabel: 'My checkbox',
        bind: {
            hidden: '{!activeRecord.phantom}'
        }
    }
]

解决方案

We ended up using the following base class for a Model, which is more convenient rather than a formula in a ViewModel.

// Use your own name instead of BaseModel

Ext.define('BaseModel', {
    extend: 'Ext.data.Model',

    fields: [{
        name: 'phantom',
        persist: false,
        convert: function (v, rec) {
            var id = rec.data[rec.idProperty];
            return !id || (Ext.isString(id) && id.indexOf(rec.entityName) == 0);
        }
    }],

    commit: function (silent, modifiedFieldNames) {
        this.data.phantom = false;
        this.callParent(arguments);
    }
});

Then you'll be able to use the binding you want

    bind: {
        hidden: '{!activeRecord.phantom}'
    }

这篇关于ViewModel绑定记录幻影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆