当前位置: 首页 > 软件库 > 程序开发 > >

ember-data-tasks

授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 佘缪文
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

ember-data-tasks

An alternative Ember Data store that returns Ember Concurrency tasks instead of promises.

Install

ember install ember-data-tasks

This addon comes in three phases:

Phase 1: Store Override

By overriding the store with a mixin, we extend the behavior of methods on the store, like store.findRecord('post', 1).

Setup

Override your existing store, by creating a new store (or use your existing one, if you have it):

ember g service store

Open app/services/store.js and modify it to:

import DS from 'ember-data';
import TaskStoreMixin from 'ember-data-tasks/mixins/task-store';

export default DS.Store.extend(TaskStoreMixin);

Use

Now you can use your Ember Data store like before, and nothing has changed, since thisstore is backwards compatible, due to the fact that tasks also adhere to the promise spec.

But you didn't come here for the same old, you want immediate results.To take advantage of the benefits of tasks, you will have to wrap your store responses in a hash.

The example below will hit afterModel after the backend has resolved with data:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('post');
  }
});

But if you want immediate responses, do the following, and afterModel willbe hit immediately:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return {
      postsTask: this.store.findAll('post')
    };
  }
});

Then you can utilize the task in your template like so:

<ul>
  {{#if model.postsTask.isRunning}}
    Loading your posts..
  {{else}}
    {{#each model.postsTask.value as |post|}}
      <li>{{post.name}}</li>
    {{/each}}
  {{/if}}
</ul>

This seems like a slight annoyance at first, due to the extra level of nesting, butin the end if you build ambitious apps, you will most likely return multipletasks, and would have used Ember.RSVP.hash anyway, if working with promises.

Note: You can unwrap the task hash in setupController, if it really bothers you.

Phase 2: Model Override

This phase allows you to enable tasks for methods like model.save().

Setup

Open app/models/my-model.js and modify it to:

import DS from 'ember-data';
import TaskModelMixin from 'ember-data-tasks/mixins/task-model';

export default DS.Model.extend(TaskModelMixin, {
  // your model definition
});

You'd have to do this for every model, and if you have few, that should be easy.For those that have many models, you can do the following:

Reopen the DS.Model in your app/app.js file.

import Ember from 'ember';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';
// Add these two imports
import DS from 'ember-data';
import TaskModelMixin from 'ember-data-tasks/mixins/task-model';

let App;

Ember.MODEL_FACTORY_INJECTIONS = true;

App = Ember.Application.extend({
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  Resolver
});

loadInitializers(App, config.modulePrefix);

// Add this model reopen
DS.Model.reopen(TaskModelMixin);

export default App;

Use

Now you can call model.reload(), model.save(), and model.destroyRecord() and gettasks back instead of promises.

 相关资料
  • ember-data-contentful This is an Ember Data adapter/serializer that uses the READ ONLY Content Delivery API from contentful Setup in your app ember install ember-data-contentful After installing the a

  • Ember Data Copyable Intelligently copy an Ember Data model and all of its relationships Features Shallow & deep copy an Ember Data model Shallow & deep copy model relationships Handles cyclical relati

  • Ember Data Github Ember Data abstraction for the GitHub REST API v3. Installation ember install ember-data-github Usage You need to choose how you wish to authenticate your GitHub requests using OAuth

  • 目前,我们的应用程序使用硬编码的数据作为租赁列表中定义的rentals路由处理程序。随着应用程序的发展,我们希望在服务器上保留我们的租用数据,并且更容易地对数据进行高级操作,如查询。 Ember提供了一个名为Ember Data的数据管理的库来帮助处理持久的应用程序数据。 Ember Data要求你通过扩展DS.Model来定义要提供给应用程序的数据的结构。 可以使用Ember CLI生成Embe

  • ember-data-url-templates ember-data-url-templates is an addon to allow building urls with url templates instead ofdefining buildURL as described in RFC #4. ember-data-url-templates is under early deve

  • Ember检查器是一个浏览器插件,用于调试Ember应用程序。 灰烬检查员包括以下主题 - S.No. 灰烬检查员方式和描述 1 安装Inspector 您可以安装Ember检查器来调试您的应用程序。 2 Object Inspector Ember检查器允许与Ember对象进行交互。 3 The View Tree 视图树提供应用程序的当前状态。 4 检查路由,数据选项卡和库信息 您可以看到检查