get
Use it to get a single instance by id.
Map.get(params)
.get()
is added to the configured Map type.
Use it to get a single instance by the identity keys of the Map type.
// import connection plugins
var canMap = require("can-connect/can/map/map");
var constructor = require("can-connect/constructor/constructor");
var dataUrl = require("can-connect/data/url/url");
// define connection type
var Todo = DefineMap.extend({
id: "number",
complete: "boolean",
name: "string"
});
// create connection
connect([canMap, constructor, dataUrl],{
Map: Todo,
url: "/todos"
})
// retrieve instance
Todo.get({id: 5}).then(function(todo){
...
});
Parameters
- params
{Object}
:Identifying parameters of the instance to retrieve. Typically, this is an object with the identity property and its value like:
{_id: 5}
.
Get a single record by filtering non-identity keys
Sometimes, you want a single record, but by filtering non-identity keys. Instead of using
.get
, use .getList
like:
var firstCompleteTodo = Todo.getList({
filter: {complete: false},
page: {start: 0, end: 0}
}).then(function(list){
return list.length ? list[0] : Promise.reject({message: "reject message"});
});