Ember model to json

后端 未结 12 1369
北恋
北恋 2020-12-13 13:55

I am looking for an efficient way to translate my Ember object to a json string, to use it in a websocket message below

/*
 * Model
 */

App.node = Ember.Obj         


        
12条回答
  •  清歌不尽
    2020-12-13 14:50

    You can get a plain JS object (or hash) from an Ember.Object instance by calling getProperties() with a list of keys.

    If you want it as a string, you can use JSON.stringify().

    For example:

    var obj  = Ember.Object.create({firstName: 'Erik', lastName: 'Bryn', login: 'ebryn'}),
        hash = obj.getProperties('firstName', 'lastName'), // => {firstName: 'Erik', lastName: 'Bryn'}
        stringHash = JSON.stringify(hash); // => '{"firstName": "Erik", "lastName": "Bryn"}'
    

提交回复
热议问题