Good morning!
due ti the fact, that I get my models by jsonRPC via a php server, my model-functions are overridden:
return can.Model.extend(
To use fixtures, I did edit the fixtures.js into
steal("can/util/fixture", function(fixture) {
var todos = fixture.store(5, function(i){
return {
id : i,
name: "todo "+i,
description: "doitnow " + i
}
});
fixture({
'/api/v1/jsonrpc.php' :function(params){
data = JSON.parse(params.data);
params.data = data.params[1];
fixtureFunc = data.params[0] + 's.' + data.method
if(data.method === 'findAll') return eval(fixtureFunc)(data.params[1])
return eval(fixtureFunc)(params,function(resp){
steal.dev.log(resp)
})
});
}
});
I use it in the moment with a scaffolded exampel like 'list.js' (working):
and (not working)
How do I have to define the callback function for findOne to get the same behaviour as with the ajax-request not using fixtures?
due ti the fact, that I get my models by jsonRPC via a php server, my model-functions are overridden:
return can.Model.extend(
- /* @static */
- {
- sequence : 100,
- model: function( attributes ) {
- if(typeof(attributes.result) !== 'object') return can.Model.model.call(this,attributes);
- else return can.Model.model.call(this,attributes.result);
- },
- models: function( instancesRawData ) {
- if(typeof(instancesRawData.result) !== 'object') return can.Model.models.call(this,instancesRawData);
- else return can.Model.models.call(this,instancesRawData.result);
- },
- queryfunc : function(id, method, params, success, error
- ){
- var returninstance = this.shortName.substr(0, 1).toLowerCase() + this.shortName.substr(1)
- return $.ajax({
- async: true,
- contentType: 'application/json',
- type: 'POST',
- processData: false,
- url: '/api/v1/jsonrpc.php',
- cache: false,
- data: JSON.stringify({jsonrpc : '2.0', method :method, params : new Array(returninstance, params) , id : (this.sequence++)})
- })
- },
- /**
- * Find all tupels
- */
- findAll : function(params, success, error){
- return this.queryfunc(null, 'findAll', params, success, error)
- },
- /**
- * Find one tupel
- */
- findOne : function(params, success, error){
- return this.queryfunc(null, 'findOne', params, success, error)
- },
- /**
- * Create a tupel
- */
- create : function(params, success, error){
- return this.queryfunc(null, 'create', params, success, error)
- },
- /**
- * Update a tupel by its ID
- */
- update : function(id, params, success, error){
- return this.queryfunc(id, 'update', params, success, error)
- },
- /**
- * destroy a tupel by its ID
- */
- destroy : function(id, success, error){
- var params = {id : id}
- return this.queryfunc(id, 'destroy', params, success, error)
- }
- },
- /* @Prototype */
- {});
- });
To use fixtures, I did edit the fixtures.js into
steal("can/util/fixture", function(fixture) {
var todos = fixture.store(5, function(i){
return {
id : i,
name: "todo "+i,
description: "doitnow " + i
}
});
fixture({
'/api/v1/jsonrpc.php' :function(params){
data = JSON.parse(params.data);
params.data = data.params[1];
fixtureFunc = data.params[0] + 's.' + data.method
if(data.method === 'findAll') return eval(fixtureFunc)(data.params[1])
return eval(fixtureFunc)(params,function(resp){
steal.dev.log(resp)
})
});
}
});
I use it in the moment with a scaffolded exampel like 'list.js' (working):
- init: function () {
- this.list = new Todo.List();
- this.element.html(initEJS(this.list));
- this.list.replace(Todo.findAll({id:2}))
- }
and (not working)
- init: function () {
- this.list = new Todo.List();
- this.element.html(initEJS(this.list));
- this.list.replace(Todo.findAll({id:2}))
- }
How do I have to define the callback function for findOne to get the same behaviour as with the ajax-request not using fixtures?