Ember.js 如何访问模型以向外部插件提供数据?

Ember.js 如何访问模型以向外部插件提供数据?,ember.js,ember-data,Ember.js,Ember Data,我正在使用datagrid的外部插件。我需要将json对象传递给外部插件 当我试图访问模型以收集数据(json对象)时,什么都没有发生,因为它是类而不是对象 我在这里列出了我的代码: 路由器: Grid.Router.map(function () { this.resource('mainview', { path: '/' }); this.resource("modal", function(){ this.route("ne

我正在使用datagrid的外部插件。我需要将json对象传递给外部插件

当我试图访问模型以收集数据(json对象)时,什么都没有发生,因为它是类而不是对象

我在这里列出了我的代码:

路由器:

        Grid.Router.map(function () {
      this.resource('mainview', { path: '/' });
        this.resource("modal", function(){
          this.route("new", {path:"/new"});
          this.route("edit", {path: "/:contact_id" });
        });           
    });  

    Grid.MainviewRoute = Ember.Route.extend({
          model: function () {
            return Grid.ModalModel.find();
          }
        });

    Grid.GriddataRoute = Ember.Route.extend({
          model: function () {
                return Grid.ModalModel.find();
              }         
}); 
Model.js:

    Grid.ModalModel =  DS.Model.extend({
    fname: DS.attr('string'),
    lname: DS.attr('string'),
    email: DS.attr('string'),
    contactno: DS.attr('string'),
    gendertype: DS.attr('boolean'),
    contactype: DS.attr('number')
});

Grid.ModalModel.FIXTURES = [
                       {
                         id: 1,
                         fname: "ghg",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 1,
                         action1:"",
                         action2:""
                       },
                       {
                         id: 2,
                         fname: "ghg",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 1,
                         action1:"",
                         action2:""
                       },
                       {
                         id: 3,
                         fname: "ghg",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 1,
                         action1:"",
                         action2:""
                       }
                      ];
Store.js:

Grid.Store = DS.Store.extend({
                  revision: 11,
                  adapter: 'DS.FixtureAdapter'
                });
控制器:

return Grid.ModalModel.find();
在这里,视图从控制器访问数据。在视图(didInsertElement)中,我调用外部datagrid函数并将数据作为参数传递给该函数

我收到以下问题:

如果我试图使用
Grid.ModalModel.find()
访问数据,我将获得类(请参阅console.log中的以下数据)

如果我使用
返回这个.get('controller.model')
我得到的是
未定义的

当我试图将
Grid.ModalModel.find()
转换为json对象时,出现以下错误:

Uncaught TypeError: Converting circular structure to JSON 
有人能告诉我如何访问这里的模型以获取json对象格式的数据吗

我正在尝试使用以下代码

Grid.MainviewController = Ember.ArrayController.extend({
    contentChanged: function() {
        this.get('content').forEach(function(item){
          // doing item.toJSON() gives you a plain JSON object
          console.log(item.toJSON({includeId:true}));
        });
      }.observes('content.@each'),
      showmodal: function(){    
            $('#modal').modal(); 
    }
}); 

但是
console.log(item.toJSON({includeId:true}))抛出以下错误
未捕获类型错误:对象[Object Object]没有方法“toJSON”
您应该为各自的路由创建一个
控制器
,并在其中定义一个绑定到
内容的计算属性
,每次内容更改时都会触发CP函数

大概是这样的:

Grid.MainviewController = Ember.ArrayController.extend({
  contentChanged: function() {
    this.get('content').forEach(function(item){
      // doing item.toJSON() gives you a plain JSON object
      console.log(item.toJSON({includeId:true}));
    });
  }.observes('content.@each')
});
然后使用
DS.Model
内置方法
toJSON()
获取记录的JSON表示

请参见演示


希望能有所帮助。

您是如何尝试将
Grid.ModalModel.find()转换为JSON的?回答得不错@inuitivePixel。我不知道
toJSON
,也没有成功地观察到内容,可能是因为我是从route controller观察的。只需一个建议,即观察
模型。IsUpdate
,触发的频率将低于
内容。@each
@Márciordriguescorreajúnior,谢谢你的建议,但你的意思是观察
模型。IsUpdate
从路由或控制器?在控制器中,就像你所做的那样。使用
.observes('model.isupdated')
代替
观察('content@each')
。在方法
的开头,如果(this.get('model.isupdate'){return;}
,则在加载时不执行。@Márciordriguescorreajúnior,奇怪。。。它不起作用。(《观察家》也从来没放过你的第一把小提琴。)我看到它起作用了。也许jsbin有问题:(
Grid.MainviewController = Ember.ArrayController.extend({
  contentChanged: function() {
    this.get('content').forEach(function(item){
      // doing item.toJSON() gives you a plain JSON object
      console.log(item.toJSON({includeId:true}));
    });
  }.observes('content.@each')
});