1 /** 2 * @fileOverview default sync for model 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("mvc/sync", function (S, io, JSON) { 6 var methodMap = { 7 'create':'POST', 8 'update':'POST', //'PUT' 9 'delete':'POST', //'DELETE' 10 'read':'GET' 11 }; 12 13 /** 14 * Default sync mechanism. 15 * Sync data with server using {@link IO} . 16 * @memberOf MVC 17 * @param {MVC.Model|MVC.Collection} self Model or Collection instance to sync with server. 18 * @param {String} method Create or update or delete or read. 19 * @param {Object} options IO options 20 */ 21 function sync(self, method, options) { 22 var type = methodMap[method], 23 ioParam = S.merge({ 24 type:type, 25 dataType:'json' 26 }, options); 27 28 var data = ioParam.data = ioParam.data || {}; 29 data['_method'] = method; 30 31 if (!ioParam.url) { 32 ioParam.url = S.isString(self.get("url")) ? 33 self.get("url") : 34 self.get("url").call(self); 35 } 36 37 if (method == 'create' || method == 'update') { 38 data.model = JSON.stringify(self.toJSON()); 39 } 40 41 return io(ioParam); 42 } 43 44 return sync; 45 }, { 46 requires:['ajax', 'json'] 47 });