2012-11-29 42 views
0

我有一个模型结构如下所示,我想知道如何使用Bookings数组作为我的DataView的数据源。如何使用关联的模型作为数据源的数据源

模型结构:

Client 
    ClientId 
    Name 
    Bookings (HasManyAssociation) 
    Contacts (HasManyAssociation) 
    AjaxProxy 
    JsonReader (ImplicitIncludes is set to true so child models are created with one call) 

Booking 
    BookingNodeId 
    BookingDetails 

Contact 
    ContactNodeId 
    ContactDetails 

上面给我的数据结构如下:

Client 
    Bookings[ 
    Booking 
    Booking 
    ] 
    Contacts[ 
    Contact 
    Contact 
    ] 

我希望能够做的是要么,从我的预订阵列创建一个存储和然后使用该存储作为我的DataView的数据源,或者直接使用Bookings数组作为数据源(我并不在乎我如何做到这一点)。

如果我在我的预订模型上设置了AjaxProxy,它工作正常,但显然我无法在加载我的JSON时自动创建我的客户端和联系人。

在我看来,有意义的是,客户端模型(作为分层结构的顶级模型)是加载数据的模型。

编辑:

我想通了如下(特别感谢以下handet87他dataview.setStore()指针)。

在这种情况下的关键是要知道创建关系实际上设置了另一个名为BookingsStore和ContactsStore的商店。 我需要做的只是dataview.setStore(“BookingsStore”)

+0

克里斯,这是我的推荐:避免关联。未来会有痛苦,真的不会使用它。 – lontivero

回答

2

使用Ajax请求从服务器获取数据,然后您可以使用解析的json数据作为数组。定义一个商店并将该数据添加到该商店。最后,将该商店分配给您的数据视图。

Ext.Ajax.request({ 
url: '/Default.aspx', 
params: { 
    'params': itemId 
}, 
failure: function (response) { 
    Ext.Msg.alert('error', 'error'); 
}, 
success: function (response, opts) { 
    var jsonData = JSON.parse(response.responseText); 
    //now you can access jsonData.Bookings 

    var store = Ext.create('Ext.data.Store', { 
     model: 'MyApp.model.Client'   
    }); 

    store.setData(jsonData.Bookings); 

    dataview.setStore(store); 
}}); 

主要的方法就是这样。希望这可以帮助。

+0

感谢您的帮助handet87,但它并不完全是我的意思(尽管您的dataview.setStore()指针值得大拇指)。我已经想通了,编辑了我的答案,以备将来帮助其他人。 –

相关问题