1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4 // Creator:   Sebastian
  5 // Date:      02.12.2010
  6 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
  7 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  8 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
  9 // ==========================================================================
 10 
 11 m_require('core/datastore/data_provider.js');
 12 
 13 /**
 14  * @class
 15  *
 16  * Encapsulates access to a remote storage, a json based webservice.
 17  *
 18  * @extends M.DataProvider
 19  */
 20 M.RemoteStorageProvider = M.DataProvider.extend(
 21 /** @scope M.RemoteStorageProvider.prototype */ {
 22 
 23     /**
 24      * The type of this object.
 25      * @type String
 26      */
 27     type: 'M.RemoteStorageProvider',
 28 
 29     /**
 30      * The type of this object.
 31      * @type Object
 32      */
 33     config: null,
 34 
 35     /* CRUD methods */
 36 
 37     save: function(obj) {
 38 
 39         var config = this.config[obj.model.name];
 40         var result = null;
 41         var dataPattern = null;
 42         var dataResult = null;
 43 
 44         if(obj.model.state === M.STATE_NEW) {   /* if the model is new we need to make a create request, if not new then we make an update request */
 45 
 46             /* the given dataPattern must be a valid JSON string that is then transformed into an object structure*/ 
 47             dataPattern = JSON.parse(config.create.dataPattern);
 48             dataResult = this.deepObjectReplacement(dataPattern, obj.model.record, result);
 49 
 50             M.Request.init({
 51                 url: config.location + config.create.url,
 52                 type: config.create.httpMethod,                
 53                 isJSON: YES,
 54                 contentType: 'application/JSON',
 55                 data: dataResult,
 56                 onSuccess: function(data) {
 57                     obj.onSuccess(data);
 58                 },
 59                 onError: function() {
 60                     obj.onError();
 61                 }
 62             }).send();
 63             
 64         } else { // make an update request
 65             dataPattern = JSON.parse(config.create.dataPattern);
 66             dataResult = this.deepObjectReplacement(dataPattern, obj.model.record, result);
 67 
 68 
 69             /* make generic */
 70             var updateURL = config.update.url.replace(/<%=\s+([.|_|-|$|�|a-zA-Z]+[0-9]*[.|_|-|$|�|a-zA-Z]*)\s*%>/, object.model.record.ID);
 71             
 72             M.Request.init({
 73                 url: config.location + updateUrl,
 74                 type: config.update.httpMethod,
 75                 isJSON: YES,
 76                 contentType: 'application/JSON',
 77                 data: dataResult,
 78                 onSuccess: function(data) {
 79                     obj.onSuccess(data);
 80                 },
 81                 onError: function() {
 82                     obj.onError();
 83                 },
 84                 beforeSend: function(xhr) {
 85                   xhr.setRequestHeader("X-Http-Method-Override", config.update.httpMethod);
 86                 }
 87             }).send();
 88 
 89         }
 90         
 91     },
 92 
 93     del: function(obj) {
 94 
 95     },
 96 
 97     find: function(obj) {
 98         var config = this.config[obj.model.name];
 99         var readUrl = obj.ID ? config.read.url.one.replace(/<%=\s+([.|_|-|$|�|a-zA-Z]+[0-9]*[.|_|-|$|�|a-zA-Z]*)\s*%>/,obj.ID) : config.read.url.all;
100 
101         console.log(config.location + readUrl+ '.json');
102 
103         M.Request.init({
104             url: config.location + readUrl + '.json',
105             type: config.read.httpMethod,
106             isJSON: YES,
107             contentType: 'application/JSON',
108             onSuccess: function(data) {
109                 obj.onSuccess(data);
110             },
111             onError: function() {
112                 obj.onError();
113             }
114         }).send();
115     },
116 
117     remoteQuery: function(onSuccess, onError) {
118         
119     },
120 
121     /**
122      * creates a new data provider instance with the passed configuration parameters
123      * @param obj
124      */
125     configure: function(obj) {
126         console.log('configure() called.');
127         // maybe some value checking
128         return this.extend({
129             config:obj
130         });
131     },
132 
133 
134     /**
135      * Creates result as an object passed to the request containing the request data.
136      *
137      * @param dataPattern
138      * @param record
139      * @param result
140      */
141     deepObjectReplacement: function(dataPattern, record, result) {
142         for(var i in dataPattern) {
143             if(typeof(dataPattern[i]) === 'object') {
144                 this.deepObjectReplacement(dataPattern[i], record, result);
145             }
146             var valueRegEx = /<%=\s+([.|_|-|$|�|a-zA-Z]+[0-9]*[.|_|-|$|�|a-zA-Z]*)\s*%>/;
147 
148             if(typeof(dataPattern[i]) != 'object') {
149                 var regExResult = valueRegEx.exec(dataPattern[i]);
150                 dataPattern[i] = record[i];
151                 console.log(dataPattern[i]);
152             }
153         }
154         return dataPattern;
155     }
156 
157 });