7.6 Models--Finding Records

Source: Internet
Author: User

The store for Ember data provides an interface for retrieving a type of records.

One, retrieving a single record (search Order Records)

1. Use Store.findrecord () to retrieve a record by type and ID. This returns a promise, which is implemented by the requested record:

var  This // = GET/POSTS/1

2. Use Store.peekrecord () to retrieve a record with type and ID, without generating a network request. This will return the record only if it already exists in the sotre.

var  This // = no network request

Second, retrieving Mutiple Records

1. For a given type, use store.findall () to retrieve all the records.

var  This // = get/posts

2. For a given type, using store.peekall () to retrieve all the records, these records have been loaded into the store without generating a network request:

var  This // = no network request
    • Store.findall () returns a DS. Promisearray, which is implemented as DS. Recordarray and Store.peekall directly returns a DS. Recordarray.
    • Note DS. Recordarray is not a JS array, which is important. It is an object that implements the ember.enumerable . This is important because, for example, if you want to retrieve the records,[] symbol by index, you must use objcetat (index) instead.

Third, querying for multiple records

Ember data provides the ability to query records that meet certain criteria. Calling Store.query () obtains a GET request and serializes the passed object into a query parameter. This method returns the DS as the find method . Promisearray.

For example, we can query all Pserson modelswith the name Peter :

var  This // = = GET To/persons?name=peter

Iv. integrating with the route ' s model Hook

1. As discussed in specifying a Route's model, routes is responsible for telling the template which model to load.

2. the Ember.route model Hook supports out-of-the-box asynchronous values. If you return a promise from the model hook , the router will wait until the promise finishes rendering the template.

3. Using ember data makes it easy to write apps with a single piece of information. Simply return the requested record from the model Hook and let Ember handle the need for a network request.

App/router.js

var Router = Ember.Router.extend ({}); Router.map (function() {  this. Route (' posts ');    this. Route (' Post ', {path: ':p ost_id 'default Router;

App/routes/posts.js

default Ember.Route.extend ({  model () {    returnthis. Store.findall (' Post ') ;  }});

App/routes/post.js

default Ember.Route.extend ({  model (params) {    returnthis. Store.findrecord (' post '), params.post_id);}  )

7.6 Models--Finding Records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.