LoginSignup
1
1

More than 5 years have passed since last update.

Ember.Objectからjsonを抽出する方法です。

  • Jsonable Mixinを定義する
App.Jsonable = Ember.Mixin.create
  getJson: (->
    ret = [] 
    for key of this
      if (@hasOwnProperty(key))
        v = this[key]
        if (v == 'toString')
          continue
        if (Ember.typeOf(v) == 'function')
          continue
        ret.push(key)
    return @getProperties.apply(this, ret)
  )
  • Ember.Object作成時にJsonableを組み込む
App.article = Ember.Object.create App.Jsonable,
  author: 'Bob',
  title: 'My first article',
  published_on: '2012-12-21',
  body: 'Hello, World!'

App.article.getJson() # => {author: 'Bob'
  • getJson() メソッドでJSON用オブジェクトを抽出できます。
JSON.stringify(App.article.getJson()) # => "{author: 'Bob', ...}"

この方法は、以下のサイトで紹介されてました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1