JavaScriptMVC: How to use model findAll to encapsulate these URLs?

223 Views Asked by At

I have following URLs:

/tasks/             // Return a list of JSON
/task/next-week/    // Return a list of JSON

I have Task model in JSMVC:

$.Model('Task', {

    findAll: 'GET /tasks/',
    findOne: 'GET /task/{id}'

});

How can I support /task/next-week/ ?

The following code:

Task.findAll({'range': 'next-week'})

results in the request /tasks/?range=next-week, but this is wrong for my project.

How could I properly encapsulate the service, so the request will look normally like /task/next-week/?

1

There are 1 best solutions below

0
On

You can specify a function for findAll and make a custom ajax request, such as what's suggested in the docs:

$.Model('Task', {

    findAll: function(params, success, error){

        if (!params.range) params.range = '';

        return $.ajax({
            url: '/tasks/' + params.range,
            type: 'get',
            dataType: 'json task.models',
            success: success,
            error: error
        });
    },

    findOne: 'GET /task/{id}'

});