zoukankan      html  css  js  c++  java
  • 7.3 Models -- Creating And Deleting Records

    一、Creating

    1. 你可以通过调用在store中的createRecord方法来创建records。

    store.createRecord('post', {
      title: 'Rails is Omakase',
      body: 'Lorem ipsum'
    });

    2. 这个store对象可以通过this.storecontrollersroutes中使用。

    3. 尽管createRecord相当简单,唯一要注意的是你不能分配一个promise作为一个关系。例如,如果你希望设置一个postauthor属性,如果user的id没有被加载进store,这将不会实现:

    var store = this.store;
    
    store.createRecord('post', {
      title: 'Rails is Omakase',
      body: 'Lorem ipsum',
      author: store.findRecord('user', 1)
    });

    4. 然而,在这个promise实现之后你可以很容易的设置关系:

    var store = this.store;
    
    var post = store.createRecord('post', {
      title: 'Rails is Omakase',
      body: 'Lorem ipsum'
    });
    
    store.findRecord('user', 1).then(function(user) {
      post.set('author', user);
    });

    二、Deleting records

    删除记录和创建记录一样简单。仅仅是调用 DS.Model任意实例上的deleteRecord()方法。这标记这个record是isDeleted并且因此从store上的all()查询上移除它。

    然后使用save()删除可以被持久化(指在数据库上也删除)。或者,你可以使用destroyRecord方法同事删除并持久化。

    store.findRecord('post', 1).then(function(post) {
      post.deleteRecord();
      post.get('isDeleted'); // => true
      post.save(); // => DELETE to /posts/1
    });
    
    // OR
    store.findRecord('post', 2).then(function(post) {
      post.destroyRecord(); // => DELETE to /posts/2
    });
  • 相关阅读:
    Windows下Goland的Terminal设置为Git Bash
    BoltDB简单使用教程
    Base64编码转换原理
    [区块链|非对称加密] 对数字证书(CA认证)原理的回顾
    [数据库锁机制] 深入理解乐观锁、悲观锁以及CAS乐观锁的实现机制原理分析
    升级mojave后的小问题解决
    ubuntu安装ssh服务记录
    dubbo+maven多模块项目单元测试
    sass与less
    (转)初识 Lucene
  • 原文地址:https://www.cnblogs.com/sunshineground/p/5165950.html
Copyright © 2011-2022 走看看