]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/ux/parse/Model.js
1 Ext
.define('Ext.ux.parse.Model', {
2 extend
: 'Ext.data.Model',
7 'Ext.ux.parse.association.Pointer',
8 'Ext.ux.parse.association.Relation'
16 getParseClass: function() {
17 if (!this.$parseClass
) {
18 this.$parseClass
= this.getName().split(".").pop();
20 return this.$parseClass
;
27 getParseObject: function() {
28 return this.$parseObject
;
30 setParseObject: function(value
) {
31 this.$parseObject
= value
;
35 getParseClass: function() {
36 if (!this.$parseClass
) {
37 var modelClass
= Ext
.ModelManager
.getModel(this.$className
);
38 this.$parseClass
= modelClass
.getParseClass();
41 return this.$parseClass
;
44 constructor: function(data
, id
, raw
, convertedData
) {
45 if (data
instanceof Parse
.Object
) {
46 this.setParseObject(data
);
47 id
= id
|| data
.id
|| null;
48 data
= data
.attributes
;
50 var _data
= raw
|| data
|| {};
51 id
= _data
.id
|| id
|| null;
52 this.setParseObject(ParseHelper
.getObject(this.getParseClass(), _data
));
55 return this.callParent([data
, id
, raw
, convertedData
]);
58 monitorRelations: function(config
) {
59 config
= config
|| {};
61 promise
= new Ext
.Promise
,
62 callback
= config
.callback
|| Ext
.emptyFn
,
63 scope
= config
.scope
|| this;
66 var status
= me
.getRelationsStatus();
68 var association
= status
.loading
.shift(),
69 store
= association
.getStore(me
);
71 store
.on("load", run
, me
, {single
:true})
74 callback
.apply(scope
);
81 getRelationsStatus: function() {
83 status
= {relations
: {}},
84 associations
= this.getAssociations(),
85 relationStatus
, relationStore
;
87 associations
.each(function(association
) {
88 if (association
.getType() === "relation" && association
.getStatus
) {
89 relationStatus
= association
.getStatus(me
);
90 relationStore
= association
.getStore(me
);
92 if(!Ext
.isArray(status
[relationStatus
])){
93 status
[relationStatus
] = []
95 status
[relationStatus
].push(association
);
96 status
.relations
[association
.getName()] = {store
:relationStore
, status
: relationStatus
};
103 relationsLoaded: function() {
104 var associations
= this.getAssociations(),
108 associations
.each(function(association
) {
109 if (association
.getType() === "relation" && association
.getStatus
) {
110 relationStatus
= association
.getStatus(this);
111 loaded
= status
=== "loading" || status
=== "unloaded";
118 getDataFlat: function() {
120 data
= Ext
.merge({}, this.data
),
121 associations
= this.getAssociations();
123 associations
.each(function(association
) {
124 if (association
.getData
) {
125 data
[association
.getName()] = association
.getData(me
);
132 load: function(options
) {
133 options
= options
|| {};
135 id
= me
.get("id") || options
.id
|| null;
137 if (id
&& id
.indexOf("ext-record") === -1) {
138 var modelClass
= Ext
.ModelManager
.getModel(me
.$className
);
139 modelClass
.load(id
, {
140 success: function(record
, operation
) {
141 me
.syncParse(me
.getFields().all
);
142 if (options
.success
) options
.success
.apply(options
.scope
|| me
, [me
, operation
]);
144 failure: function(record
, operation
) {
145 if (options
.failure
) options
.failure
.apply(options
.scope
|| me
, [me
, operation
]);
150 Ext
.Logger
.warn('You cannot load Parse models without a Parse ID');
155 syncParse: function(fields
) {
156 var me
= this, value
;
158 Ext
.Array
.forEach(fields
, function(field
) {
160 value
= me
.get(field
.getName());
161 field
= field
.getName();
163 value
= me
.get(field
);
166 if (value
&& value
.isParseModel
) {
167 value
= value
.getParseObject();
170 me
.$parseObject
.set(field
, value
);
174 afterEdit: function(modifiedFieldNames
, modified
) {
175 this.callParent(arguments
);
176 this.syncParse(modifiedFieldNames
);
180 * Sets the given field to the given value, marks the instance as dirty.
181 * @param {String/Object} fieldName The field to set, or an object containing key/value pairs.
182 * @param {Object} value The value to set.
184 set: function(fieldName
, value
) {
186 // We are using the fields map since it saves lots of function calls
187 fieldMap
= me
.fields
.map
,
188 modified
= me
.modified
,
189 notEditing
= !me
.editing
,
191 modifiedFieldNames
= [],
192 field
, key
, i
, ln
, currentValue
, convert
;
195 * If we're passed an object, iterate over that object. NOTE: we pull out fields with a convert function and
196 * set those last so that all other possible data is set before the convert function is called
198 if (arguments
.length
== 1) {
199 for (key
in fieldName
) {
200 if (fieldName
.hasOwnProperty(key
)) {
201 //here we check for the custom convert function. Note that if a field doesn't have a convert function,
202 //we default it to its type's convert function, so we have to check that here. This feels rather dirty.
203 field
= fieldMap
[key
];
204 if (field
&& field
.hasCustomConvert()) {
205 modifiedFieldNames
.push(key
);
209 if (!modifiedCount
&& notEditing
) {
214 if (!field
) field
= this.get(key
);
215 if (field
&& field
.isModel
) {
216 field
.set(fieldName
[key
]);
218 me
.set(key
, fieldName
[key
]);
223 ln
= modifiedFieldNames
.length
;
225 if (!modifiedCount
&& notEditing
) {
229 for (i
= 0; i
< ln
; i
++) {
230 field
= modifiedFieldNames
[i
];
231 me
.set(field
, fieldName
[field
]);
236 if (notEditing
&& modifiedCount
) {
237 me
.endEdit(false, modifiedFieldNames
);
239 } else if (modified
) {
240 field
= fieldMap
[fieldName
];
241 convert
= field
&& field
.getConvert();
243 value
= convert
.call(field
, value
, me
);
246 currentValue
= me
.data
[fieldName
];
247 if (currentValue
&& currentValue
!= value
) {
248 me
.fireEvent("fieldupdate", this, value
, fieldName
);
250 me
.data
[fieldName
] = value
;
252 if (field
&& !me
.isEqual(currentValue
, value
)) {
253 if (modified
.hasOwnProperty(fieldName
)) {
254 if (me
.isEqual(modified
[fieldName
], value
)) {
255 // the original value in me.modified equals the new value, so the
256 // field is no longer modified
257 delete modified
[fieldName
];
258 // we might have removed the last modified field, so check to see if
259 // there are any modified fields remaining and correct me.dirty:
261 for (key
in modified
) {
262 if (modified
.hasOwnProperty(key
)) {
270 // We only go one level back?
271 modified
[fieldName
] = currentValue
;
276 me
.afterEdit([fieldName
], modified
);
280 if (this.dirty
) this.fireEvent("dirty", this);