store
Creates a store.
fixture.store(baseItems, queryLogic)
Create a store that starts with baseItems
for a service layer
described by queryLogic
.
import {DefineMap, QueryLogic, fixture, ajax} from "can";
import {Todo} from "https://unpkg.com/can-demo-models@5";
// Describe the services parameters:
const todoQueryLogic = new QueryLogic(Todo);
// Create a store with initial data.
// Pass an empty Array (ex: []) if you want it to be empty.
const todoStore = fixture.store( [
{
id: 1,
name: "Do the dishes",
complete: true
}, {
id: 2,
name: "Walk the dog",
complete: false
}
], todoQueryLogic );
// Hookup urls to the store:
fixture( "/todos/{id}", todoStore );
ajax( {url: "/todos/1"} ).then( result => {
console.log( result );
} );
Parameters
- baseItems
{Array}
:An array of items that will populate the store.
- QueryLogic
{can-query-logic}
:A description of the service layer's parameters.
Returns
{Store()}
:
A store that can be used to simulate a restful service layer that supports filtering, pagination, and more.
fixture.store(count, makeItems, queryLogic)
Similar to fixture.store(baseItems, queryLogic)
, except that
it uses makeItems
to create count
entries in the store.
import {DefineMap, QueryLogic, fixture, ajax} from "can";
import {Todo} from "https://unpkg.com/can-demo-models@5";
import "//unpkg.com/jquery@3.3.1/dist/jquery.js";
// Describe the services parameters:
const todoQueryLogic = new QueryLogic(Todo);
// Create a store with initial data.
const todoStore = fixture.store(
1000,
( i ) => ( {
id: i + 1,
name: "Todo " + i,
complete: fixture.rand( [ true, false ], 1 )[ 0 ]
} ),
todoQueryLogic
);
// Hookup urls to the store:
fixture( "/todos/{id}", todoStore );
ajax( {url: "/todos/3"} ).then( result => {
console.log( result ); //-> "{'_id':3,'name':'Todo 2','complete':true||false}"
} );
Parameters
- count
{Number}
:The number of
baseItems
to create. - makeItems
{function}
:A function that will generate
baseItems
- queryLogic
{can-query-logic}
:A description of the service layer's parameters.
Returns
{Store()}
:
A store that can be used to simulate a restful service layer that supports filtering, pagination, and more.