Skip to content

Commit

Permalink
- added custom method for collection
Browse files Browse the repository at this point in the history
- all custom methods as object {method:'nameOfRPCMethod',values:{values as object}}
  • Loading branch information
andy committed Feb 11, 2013
1 parent 1ad6823 commit 2c36a6b
Showing 1 changed file with 63 additions and 28 deletions.
91 changes: 63 additions & 28 deletions src/backbone.rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
// check if we have a non std. content-type
this.contentType = options !== undef && options.contentType !== undef ? options.contentType : this.contentType;
// fix issue with the loss of this
_.bindAll(this);
_.bindAll(this, "exceptions", "onSuccess","onError","query","checkMethods", "defaultExceptionHandler", "handleExceptions");
},
// store the old Backbone.Model constructor for later use
oldConst = Backbone.Model.prototype.constructor,
oldConstColl = Backbone.Collection.prototype.constructor,
// store the old Backbone.sync method for later use
oldSync = Backbone.sync,
// storage object to keep track of changes from the loaded objects
Expand Down Expand Up @@ -198,39 +199,48 @@

// execute a single call
if (deeperNested !== true) {
def = _.clone(definition);
exec = def.shift();
if (def.length > 0) {
_.each(def, function (param) {
if (param === '') {
//valuableDefinition.push('');
} else {
if (model instanceof Backbone.Collection) {
if (model[param] !== undef) {
if (_.isFunction(model[param])) {
valuableDefinition[param] = model[param]();
if(_.isObject(definition)){
exec = definition.method;
if(_.isUndefined(definition.values)){
valuableDefinition = {};
}else{
valuableDefinition = definition.values;
}
}else{
def = _.clone(definition);
exec = def.shift();
if (def.length > 0) {
_.each(def, function (param) {
if (param === '') {
//valuableDefinition.push('');
} else {
if (model instanceof Backbone.Collection) {
if (model[param] !== undef) {
if (_.isFunction(model[param])) {
valuableDefinition[param] = model[param]();
} else {
valuableDefinition[param] = model[param];
}
} else {
valuableDefinition[param] = model[param];
}
} else {
if (options[param] !== undef) {
valuableDefinition[param] = options[param];
if (options[param] !== undef) {
valuableDefinition[param] = options[param];
}
}
}
} else {
if (model.get(param) !== undef) {
valuableDefinition[param] = model.get(param);
} else {
if (options[param] !== undef) {
valuableDefinition[param] = options[param];
if (model.get(param) !== undef) {
valuableDefinition[param] = model.get(param);
} else {
if (options[param] !== undef) {
valuableDefinition[param] = options[param];
}
}
}
}
}
});
} else {
valuableDefinition = [];
}
});
} else {
valuableDefinition = {};
}
}

return cb(exec, valuableDefinition, scb, ecb);
}
Expand Down Expand Up @@ -315,6 +325,31 @@
oldConst.apply(this, arguments);
}
});

// overwrite backbones model constructor
Backbone.Collection = Backbone.Collection.extend({
// TODO: Document
constructor: function (collection) {
// check if the model has the rpc property and methods defined
if (this.rpc !== undef && _.isFunction(this.rpc.invoke) === true && this.methods !== undef) {
// walk through the methods
_.each(this.methods, _.bind(function (method, signature) {
// check if we have a 'non standard' signature
if ({'read': 1/*, 'create': 1, 'remove': 1, 'update': 1*/}[signature] !== 1) {
// generate the method for the signature
this[signature] = _.bind(function (options) {
// invoke the dynamicly created method
this.rpc.invoke(signature, this, options);
return this;
}, this);
}
}, this));
}

// call the original constructor
oldConstColl.apply(this, arguments);
}
});

// overwrite backbones sync
Backbone.sync = (function (Rpc) {
Expand Down

1 comment on commit 2c36a6b

@Pappa
Copy link

@Pappa Pappa commented on 2c36a6b Feb 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit makes a test app I'm writing fail. I get data returned if I use the previous commit. The model's custom exception handler is called, but passed no exception object.

Here's the model:

var TextModel = Backbone.Model.extend({
url: '/xbmc',
namespace: 'VideoLibrary',
rpc: new Backbone.Rpc({
exceptionHandler: function (exception) {
console.log("Rpc.exceptionHandler");
if (exception) {
console.log("Exception code: " + exception.code + " - message: " + exception.message);
}
},
namespaceDelimiter: '.'
}),
methods: {
read : ["GetRecentlyAddedMovies"]
}
});

Please sign in to comment.