PDA

View Full Version : About Computable Property and Data Store



RobinQu
27 Nov 2010, 5:47 AM
I've been evaluating frameworks for a mobile project.

Sproutcore is powerful and it perfectly appeals to me. However, the mobile edition of sproutcore is remote and apps using Sproutcore desktop is nightmares for phones.

So I'm trying Sencha Touch.

Yes, you are doing great job in encapsulate views. All common views can be used right out of box.
But something underneath puzzles me a lot - is there any way to add a computable property to a model.

e.g.


Ext.regModel('Contact', {
fields: [
{
name: 'firstName',
type: 'string'
},
{
name: 'lastName',
type: 'string'
},
{
name: 'mobileNum',
type: 'string'
},
{
name: 'eMail',
type: 'string'
},
"search"
]
// fields
});

i hope all fields except for "search" will sync with servers, as the value of "search" is to be generated locally, by combining all other fields, serving as the index during searching.

This requires me to maintain the "search" field every time I add a record to store or modify any field of a record.
It's too difficult to do this outside the frameworks, I suppose.

Is there any way to maintain such auto-computed field in Sencha?
Sencha is also MVC-ready , so I believe there must be some solutions out there.

BTW it's too easy to do this in Sproutcore......

Steffen Hiller
27 Nov 2010, 7:27 AM
I never used it, but I think that's what you're looking for.
Check the second config option "convert" of the Ext.data.Field class: http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.Field

So I guess your field definition for search would look something like:


{
name: 'search',
convert: function(value, record) { return record.firstName + record.lastName + etc. }
}

RobinQu
27 Nov 2010, 8:27 PM
thx, that's what i needed~

and i recommend you try out Sproutcore~
it's a completely different kind of frameworks compared to Sencha or YUI.

Steffen Hiller
27 Nov 2010, 8:33 PM
Cool.

Yeah, at first glance Sproutcore looked nice to me, since a saw a lot of parallels with Ruby on Rails. But after digging a bit deeper, the API seemed a bit strange to me.

I'm curious though how you would define the above model in Sproutcore?

Thanks.

RobinQu
27 Nov 2010, 8:45 PM
the model is sth like this

Cm.Contact = SC.Record.extend(
/** @scope Cm.Contact.prototype */ {
name: SC.Record.attr(String),
tel: SC.Record.attr(String),
avatar: SC.Record.attr(String, { defaultValue: static_url("image/avatar.png") }),
mail: SC.Record.attr(String),
chat: SC.Record.attr(String),
address: SC.Record.attr(String),
birth: SC.Record.attr(SC.DateTime, { format: '%d/%m/%Y %H:%M:%S', defaultValue: SC.DateTime.create({ year: 1988, month: 8, day: 6 }) }),

searchRelevance: 0, // a property that others may use
searchFullName: function() {
return this.get("name") + " " + this.get("mail") + " " + this.get("address");
}.property("name", "mail", "address").cacheable()
}) ;

Steffen Hiller
27 Nov 2010, 8:56 PM
Hmm yeah, such chaining like you got at the end was one thing I didn't really like.
But don't want to start a Sencha Touch vs. Sproutcore discussion here. ;)

Hope you have fun with Sencha Touch, just like I do. :)

evant
28 Nov 2010, 12:04 AM
I believe we have an enhancement request open for "virtual fields" which will do what you describe in a more simple manner.