-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from canjs/schema
Document additional signatures for fixture.store
- Loading branch information
Showing
3 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,85 @@ | |
a restful service layer that supports filtering, pagination, and | ||
more. | ||
|
||
@signature `fixture.store(baseItems, Type)` | ||
|
||
Create a store that starts with `baseItems` for a service layer for a `Type`. | ||
|
||
```js | ||
import {DefineMap, fixture, ajax} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
|
||
// 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 | ||
} | ||
], Todo ); | ||
|
||
// Hookup urls to the store: | ||
fixture( "/todos/{id}", todoStore ); | ||
|
||
ajax( {url: "/todos/1"} ).then( result => { | ||
console.log( result ); | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 6-16 | ||
|
||
@param {Array} baseItems An array of items that will populate the store. | ||
@param {function(){}} Type Any Type with a [can-reflect.getSchema] symbol. | ||
@return {can-fixture/StoreType} A store that can be used to simulate | ||
a restful service layer that supports filtering, pagination, and | ||
more. | ||
|
||
@signature `fixture.store(baseItems, schema)` | ||
|
||
Create a store that starts with `baseItems` for a service layer described by [schema can-reflect.getSchema]. | ||
|
||
```js | ||
import {DefineMap, fixture, ajax, Reflect} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
|
||
// Store the schema, so it can be modified if so desired. | ||
const schema = Reflect.getSchema(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 | ||
} | ||
], schema ); | ||
|
||
// Hookup urls to the store: | ||
fixture( "/todos/{id}", todoStore ); | ||
|
||
ajax( {url: "/todos/1"} ).then( result => { | ||
console.log( result ); | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 9-19 | ||
|
||
@param {Array} baseItems An array of items that will populate the store. | ||
@param {can-reflect.getSchema} A schema of the keys and identities for this type. | ||
@return {can-fixture/StoreType} A store that can be used to simulate | ||
a restful service layer that supports filtering, pagination, and | ||
more. | ||
|
||
@signature `fixture.store(count, makeItems, queryLogic)` | ||
|
||
Similar to `fixture.store(baseItems, queryLogic)`, except that | ||
|
@@ -86,3 +165,84 @@ | |
@return {can-fixture/StoreType} A store that can be used to simulate | ||
a restful service layer that supports filtering, pagination, and | ||
more. | ||
|
||
@signature `fixture.store(count, makeItems, Type)` | ||
|
||
Similar to `fixture.store(baseItems, Type)`, except that | ||
it uses `makeItems` to create `count` entries in the store. | ||
|
||
```js | ||
import {DefineMap, fixture, ajax} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
import "//unpkg.com/[email protected]/dist/jquery.js"; | ||
|
||
// 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 ] | ||
} ), | ||
Todo | ||
); | ||
|
||
// 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}" | ||
} ); | ||
|
||
``` | ||
@codepen | ||
@highlight 6-14 | ||
|
||
@param {Number} count The number of `baseItems` to create. | ||
@param {function} makeItems A function that will generate `baseItems` | ||
@param {function(){}} Type A type that implements [can-reflect.getSchema]. | ||
@return {can-fixture/StoreType} A store that can be used to simulate | ||
a restful service layer that supports filtering, pagination, and | ||
more. | ||
|
||
@signature `fixture.store(count, makeItems, schema)` | ||
|
||
Similar to `fixture.store(baseItems, schema)`, except that | ||
it uses `makeItems` to create `count` entries in the store. | ||
|
||
```js | ||
import {DefineMap, fixture, ajax, Reflect} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
import "//unpkg.com/[email protected]/dist/jquery.js"; | ||
|
||
// Get the schema, usually in order to modify it. | ||
const schema = Reflect.getSchema(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 ] | ||
} ), | ||
schema | ||
); | ||
|
||
// 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}" | ||
} ); | ||
|
||
``` | ||
@codepen | ||
@highlight 9-17 | ||
|
||
@param {Number} count The number of `baseItems` to create. | ||
@param {function} makeItems A function that will generate `baseItems` | ||
@param {can-query-logic} queryLogic A description of the service layer's parameters. | ||
@return {can-fixture/StoreType} A store that can be used to simulate | ||
a restful service layer that supports filtering, pagination, and | ||
more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters