Question

I cannot find a way to retrieve data from the server and convert them to model instance. I folloed the instructions here but it's still doesn't work. Here is my setup:

url for the service: services/foo/barOne. The response is : {"calendar":1352793756534,"objectId":"id2"}

The model is defined as follow:

var myModel = can.Model(
  model: function(raw) {
    newObject = {id:raw.objectId};
    return can.Model.model.call(this,newObject);
  },

  findOne: {
    url: 'services/foo/barOne',
    datatype: 'json'
  }

)

And here is how I use that:

myBar = myModel.findOne()
myBar.then(function() {
  console.log("resolved with arguments:",arguments); //[undefined]
})

I put various log and tracked all of function called, the request is correctly done, and the ajax resolved correctly. The model method is also correct and return an object of type Constructor with the right parameters. But after that, inside the pipe function of canjs, the result is lost (ie, I got the result undefined when d.resolve.apply(d, arguments) is called)

What is wrong with this scenario ? I am using canJS with jquery version 1.0.7

Was it helpful?

Solution

I don't think you need a custom converter just for changing the id. You can change it by setting the static id property. Try it like this:

var MyModel = can.Model({
  id : 'objectId'
}, {
  findOne: 'services/foo/barOne'
});

MyModel.findOne({ id : 'test' }).done(function(model) {
  console.log(model);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top