1

I'm working with an API over which I have no control. I would like to do something like this:

var Page = can.Model.extend({
  destroy: 'DELETE /api/{account_id}/{page_id}'
})

This doesn't work - canjs simply doesn't use the destroy URL. I tried creating a function, but the only param passed is the 'id'. I'm sure you'll say that this is not really REST, but I'm stuck with the API. Any time I put more than one param into the url, the url is not used.

Any ideas?

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
nick lang
  • 65
  • 4

2 Answers2

1

You're actually setting the prototype destroy property to a string here, because the first object passed to extend() is interpreted as the prototype properties if a second object is not passed. You actually need to do this: var Page = can.Model.extend({ destroy: 'DELETE /api/{account_id}/{page_id}' }, {})

(NB: CanJS internally converts destroy and some other properties from AJAX specs to functions when you extend can.Model, but only in the static properties)

air_hadoken
  • 611
  • 4
  • 5
0

It seems this is OK (took a while to figure out that the 2nd parameter is the instance... didn't see that documented anywhere):

var Page = can.Model.extend({
  destroy: function(id, page) {
    return $.get('/api/'+page.account_id+'/'+page.id);
  }
})

Which seems a bit weird, but I'll get over it!

nick lang
  • 65
  • 4