Skip to content

Commit

Permalink
Merge pull request #3 from maxsdw/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
maxsdw authored Jun 4, 2017
2 parents 86656d7 + c910046 commit 3dd031e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ Finally, in models you are able to setup jQuery.ajax() extra settings, use "ajax
config: {
url: 'api/active_user',
sendMethod: 'POST',
ajaxConf: {
api/active_user ajaxConf: {
dataType: 'xml',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
}
Expand All @@ -461,7 +461,36 @@ Finally, in models you are able to setup jQuery.ajax() extra settings, use "ajax
}
})(), {} );

*Note:* You can perform CRUD and sendData operations with local data as well, in that case any AJAX call will be executed, but the model data will be updated.
*Note:* You can perform CRUD and sendData operations with local data as well, in that case any AJAX call will be executed, but the model data will be updated.

#### URL templates

You will find cases where the resource id needs to be included in the URL on POST, GET or DELETE methods, and not necesarily at the end, in order to properly included the id in the request URL, you will need to use URL templates, lets consider the following example:

We need to send a GET request to the following URL:

api/folders/{folder_id}/items
Where {folder_id} needs to be replaced on the fly with the correct resource id everytime we want to fetch data from the server, in that case we will need to specify a model with the following characteristics:

vRap.Actions.define( 'DemoApp.models.FolderItems', (function() {
return {
extend: 'Base.primitives.Model',
config: {
url: 'api/folders/{folder_id}/items'
},
init: function() {
}
}
})(), {} );
Notice how we included "{folder_id}" inside the URL string, that will be replaced with the real resource id when executing the CRUD method, in the above example, to perform the GET request, what we will need to run is the following:

var folderItems = vRap.Actions.create( 'DemoApp.models.FolderItems', 'models.folderItems', {} );
folderItems.getData( {}, null, { folder_id: 1234 });
Notice how we are passing the template object as third argument of the function, specifying the value to use as replacement for "folder_id" inside the URL, above instruction will execute a GET request to "api/folders/1234/items".

### Defining a view

Expand Down
2 changes: 1 addition & 1 deletion src/gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function ( grunt ) {
preserveComments: 'some'
},
files: {
'vrap-js-1.0.7.min.js': [
'vrap-js-1.0.8.min.js': [
'packages/framework.js',
'packages/locale/eng-us.js',
'packages/**/*.js'
Expand Down
24 changes: 15 additions & 9 deletions src/packages/classes/primitives/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ vRap.Actions.define( 'Base.primitives.Model', (function() {

return deferred.promise();
},
_ajaxCall: function( action, defaultMethod, dataObj, beforeRefresh ) {
_ajaxCall: function( action, defaultMethod, dataObj, beforeRefresh, urlTemplate ) {
var self = this,
apiAction,
ajaxConf,
Expand All @@ -44,6 +44,12 @@ vRap.Actions.define( 'Base.primitives.Model', (function() {
dataObj = JSON.stringify( dataObj );
}

if ( urlTemplate ) {
for ( var x in urlTemplate ) {
self.config.url = self.config.url.replace( '{' + x + '}', 0 );
}
}

if ( self.config.url || apiAction ) {
ajaxConf = {
url: ( apiAction ) ? apiAction.url : self.config.url,
Expand Down Expand Up @@ -118,17 +124,17 @@ vRap.Actions.define( 'Base.primitives.Model', (function() {

return deferred;
},
getData: function( dataObj, beforeRefresh ) {
getData: function( dataObj, beforeRefresh, urlTemplate ) {
var self = this;

return self._ajaxCall( 'read', 'GET', dataObj, beforeRefresh );
return self._ajaxCall( 'read', 'GET', dataObj, beforeRefresh, urlTemplate );
},
sendData: function( dataObj ) {
sendData: function( dataObj, urlTemplate ) {
var self = this;

return self._ajaxCall( 'sendData', 'POST', dataObj || self.properties.data );
return self._ajaxCall( 'sendData', 'POST', dataObj || self.properties.data, null, urlTemplate );
},
sendRecord: function( dataObj ) {
sendRecord: function( dataObj, urlTemplate ) {
var self = this,
action,
method;
Expand All @@ -147,9 +153,9 @@ vRap.Actions.define( 'Base.primitives.Model', (function() {
method = 'POST';
}

return self._ajaxCall( action, method, dataObj );
return self._ajaxCall( action, method, dataObj, null, urlTemplate );
},
deleteRecord: function( recordId ) {
deleteRecord: function( recordId, urlTemplate ) {
var self = this,
dataObj = null;

Expand All @@ -161,7 +167,7 @@ vRap.Actions.define( 'Base.primitives.Model', (function() {
};
}

return self._ajaxCall( 'delete', 'DELETE', dataObj );
return self._ajaxCall( 'delete', 'DELETE', dataObj, null, urlTemplate );
}
};
})(), {} );
Loading

0 comments on commit 3dd031e

Please sign in to comment.