store

  • function
can.fixture.store  

Make a store of objects to use when making requests against fixtures.

can.fixture.store(count, make[, filter])

Parameters

  1. count {Number}

    The number of items to create.

  2. make {function()}

    A function that will return the JavaScript object. The make function is called back with the id and the current array of items.

  3. filter {function()}Optional

    A function used to further filter results. Used for to simulate server params like searchText or startDate. The function should return true if the item passes the filter, false otherwise. For example:

    function(item, settings){
        if(settings.data.searchText){
            var regex = new RegExp("^"+settings.data.searchText)
            return regex.test(item.name);
        }
    }
    

Returns

{can.fixture.Store}

A generator object providing fixture functions for findAll, findOne, create, update and destroy.

can.fixture.store(items[, comparator])

Parameters

  1. items {Array}

    An array of JavaScript objects that represent data from response.

  2. comparator {Object}Optional

    An object that specifies how to compare properties. Check can.Object.same for more details.

Returns

{can.fixture.Store}

A generator object providing fixture functions for findAll, findOne, create, update and destroy.

can.fixture.store(count, generator(index,items)) is used to create a store of items that can simulate a full CRUD service. Furthermore, the store can do filtering, grouping, sorting, and paging.

Basic Example

The following creates a store for 100 todos:

var todoStore = can.fixture.store(100, function(i){
    return {
        id: i,
        name: "todo number "+i,
        description: "a description of some todo",
        ownerId: can.fixture.rand(10)
    }
})

todoStore's methods:

Can be used to simulate a REST service like:

can.fixture({
    'GET /todos':         todoStore.findAll,
    'GET /todos/{id}':    todoStore.findOne,
    'POST /todos':        todoStore.create,
    'PUT /todos/{id}':    todoStore.update,
    'DELETE /todos/{id}': todoStore.destroy
});

These fixtures, combined with a can.Model that connects to these services like:

var Todo = can.Model.extend({
    findAll : 'GET /todos',
    findOne : 'GET /todos/{id}',
    create  : 'POST /todos',
    update  : 'PUT /todos/{id}',
    destroy : 'DELETE /todos/{id}'
}, {});

... allows you to simulate requests for all of owner 5's todos like:

Todo.findAll({ownerId: 5}, function(todos){

})

Example with items and comparator

The following creates a store with three items:

can.fixture({
    "/team": can.fixture.store([
        {id: 1, name: "Veljko", age: 30, city: "Zagreb"},
        {id: 2, name: "Nikica", age: 29, city: "Travnik"},
        {id: 3, name: "Mihael", age: 29, city: "Zagreb"}
    ], {name: "i", city: null}).findAll
});

var Team = can.Model.extend({
    findAll : 'GET /team'
    // other endpoints ...
}, {});

Comparator defines following rules: name attribute is compared case-insensitively, city attribute is ignored while other attributes are compared by default.

Team.findAll({}, function(data) {
    // all three objects are returned
});

Team.findAll({age: 29}, function(data) {
    // [{id: 2, name: "Nikica", age: 29, city: "Travnik"},
    //  {id: 3, name: "Mihael", age: 29, city: "Zagreb"}]
});

Team.findAll({age: 29, name: 'veljko'}, function(data) {
    // name check is case-insensitive!
    // [{id: 1, name: "Veljko", age: 30, city: "Zagreb"}]
});

Team.findAll({age: 30, city: 'Chicago'}, function(data) {
    // city attribute is ignored
    // [{id: 1, name: "Veljko", age: 30, city: "Zagreb"}]
});