mercredi 7 janvier 2015

Data binding and associated objects via MVVM

Hi,

I've been trying for some time to bind fields within a view to data model instance fields via an association and I'd like first to understand if this behaviour is supported by the ext js 5.1.

The scenario which I am sure must be common is as follows:


Customer has an address that I want to display and edit on the same panel.

I have created 5 classses: Customer, Address, CustomerPanel, CustomerPanelContainer, CustomerViewModel.


I have defined a customer class as a subclass of Ext.data.Model that has a number of instance variables, such as firstName, lastName.

I have defined an address class with instance variables address1, address2, city, postCode.

Address has an id field called "address"

I have defined an association between Customer and Address via a field on Customer:

{name: 'address', reference: 'App.model..Address', unique: true}

I have created a view model with a data config of "cust":

data: { cust: null}

I have a CustomerPanel with text fields with binds as follows:

{cust.firstName} to bind to variables defined on Customer

{cust.address.address1} etc to bind to variables held on address.

In the controller I've created a customer instance and set its variable, created an address and set its variables then added the address to the customer using setAddress(). I set the data on the view model then open the Customer Panel.

The fields displaying address data show the right values (hooray).

However when I update the values in the fields the address model are not update. When I inspect the objects in my browser console is see that my customer has a property of "_address" that holds the original values in what looks a fully formed extjs object of the right class. When I edit the data in view panel a new property is added to the customer of "address" (ie without any underscore) which has a variable for the field I've changed. This is not an instance of my address class, just a basic data object. I've been struggling with this for sometime, I am doing something that is not supported? Do I need to "unpack" the associations to in effect flatten the data before I can use my binds? If this doesn't work then it gives us a headache, our data retrieved from a server side service has a lot of nested data which we plan to represent via associations. We will have to a lot more code to flatten these if we cant use binds with associations.

Thanks a lot.






Data binding and associated objects via MVVM

Aucun commentaire:

Enregistrer un commentaire