javascriptmodel-view-controllercanjscanjs-model

CanJs Model Service Method Implementations


I am having trouble understanding how to correctly implement the can.Model service methods. I currently have this

var Foo = can.Model({
findAll: "GET /service/Editor.svc/foo",
findOne: "GET /service/Editor.svc/foo/{id}",
create: 'POST /service/Editor.svc/foo"',
update: 'PUT /service/Editor.svc/foo"{id}',
destroy: 'DELETE /service/Editor.svc/foo"{id}'
},{});

I have looked at http://canjs.com/guides/Models.html and http://canjs.com/guides/Tutorial.html and CanJS Model findAll returns list of duplicate items

I guess what I don't understand is how the model works. Is the only thing I need for these methods the above declaration? when I try to run

var test = new Foo() then foo.findAll({}, function(success){//dosomething}, function(xhr){//do something else}) I get TypeError: Object #<Constructor> has no method 'findAll'

What part of this framework am I missing?

For the record this interacts with a c# wcf service and which pulls json objects from a mongodb


Solution

  • The findAll etc. methods you are defining are constructor methods not prototype methods. That means that you have to call it on the Foo object and not the foo instance (I usually name my Model and instances of it differently as not to confuse them - e.g. var bar = new Foo()).

    Foo.findAll({}, function(data) {
    
    });
    
    // Or a little nicer:
    Foo.findAll({}).then(function(data) {
    
    });