0

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

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
Geekingfrog
  • 191
  • 1
  • 8

1 Answers1

1

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);
});
Daff
  • 43,734
  • 9
  • 106
  • 120
  • Your example 'should' work. If I only want to do name translation it's fine, but I want to do some processing before wrapping the data in the model. And also there is a problem in can.jquery.js (cf my post on the canJS [forum](https://forum.javascriptmvc.com/#Topic/32525000001079183) – Geekingfrog Nov 15 '12 at 04:17
  • Mark as solved. My version of can.js wasn't up to date. USing the latest one fixed the problem. Full thread [here](https://forum.javascriptmvc.com/topic/custom-model-method#32525000001081163) – Geekingfrog Nov 16 '12 at 01:19