Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

using fixtures in a different way?

$
0
0
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(
  1.     /* @static */
  2.     {
  3.                 sequence : 100,
  4.                
  5.                 model: function( attributes ) {
  6.                             if(typeof(attributes.result) !== 'object') return can.Model.model.call(this,attributes);
  7.                             else return can.Model.model.call(this,attributes.result);
  8.                             },
  9.                 models: function( instancesRawData ) {
  10.                             if(typeof(instancesRawData.result) !== 'object') return can.Model.models.call(this,instancesRawData);
  11.                             else return can.Model.models.call(this,instancesRawData.result);
  12.                             },
  13.                 queryfunc : function(id, method, params, success, error
  14.                         ){
  15.                     var returninstance = this.shortName.substr(0, 1).toLowerCase() + this.shortName.substr(1)
  16.                     return $.ajax({
  17.                                 async: true,
  18.                                 contentType: 'application/json',
  19.                                 type: 'POST',
  20.                                 processData: false,
  21.                                 url: '/api/v1/jsonrpc.php',
  22.                                 cache: false,
  23.                                 data: JSON.stringify({jsonrpc : '2.0', method :method, params : new Array(returninstance, params) , id : (this.sequence++)})
  24.                                     })
  25.                    
  26.                 },
  27.                
  28.         /**
  29.           * Find all tupels
  30.          */
  31.         findAll : function(params, success, error){
  32.                     return this.queryfunc(null, 'findAll', params, success, error)
  33.                 },
  34.         /**
  35.           * Find one tupel
  36.          */
  37.         findOne : function(params, success, error){
  38.                     return this.queryfunc(null, 'findOne', params, success, error)
  39.                 },
  40.         /**
  41.           * Create a tupel
  42.          */
  43.         create : function(params, success, error){
  44.                     return this.queryfunc(null, 'create', params, success, error)
  45.                 },
  46.         /**
  47.          * Update a tupel by its ID
  48.          */
  49.         update : function(id, params, success, error){
  50.                     return this.queryfunc(id, 'update', params, success, error)
  51.                 },
  52.         /**
  53.          * destroy a tupel by its ID
  54.          */
  55.         destroy : function(id, success, error){
  56.                     var params = {id : id}
  57.                     return this.queryfunc(id, 'destroy', params, success, error)
  58.                 }
  59.     },
  60.     /* @Prototype */
  61.     {});
  62. });

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):

  1. init: function () {
  2.             this.list = new Todo.List();
  3.             this.element.html(initEJS(this.list));
  4.             this.list.replace(Todo.findAll({id:2}))
  5.         }

and (not working)

  1. init: function () {
  2.             this.list = new Todo.List();
  3.             this.element.html(initEJS(this.list));
  4.             this.list.replace(Todo.findAll({id:2}))
  5.       }

How do I have to define the callback function for findOne to get the same behaviour as with the ajax-request not using fixtures?

       



Viewing all articles
Browse latest Browse all 3491

Trending Articles