0

我正在使用 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();
              }         
}); 

模型.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:""
                       }
                      ];

商店.js:

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

控制器 :

return Grid.ModalModel.find();

在这里,视图正在从控制器访问数据。在视图(didInsertElement)中,我正在调用外部数据网格函数并将数据作为参数传递给该函数。

我收到以下问题:

如果我尝试使用Grid.ModalModel.find()我正在获取类来访问数据(请参阅以下来自 console.log 的数据)

Class {type: function, store: Class, isLoaded: true, isUpdating: true, toString: function…}

如果我使用return this.get('controller.model')我得到undefined

当我尝试转换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}));抛出以下错误Uncaught TypeError: Object [object Object] has no method 'toJSON'

4

1 回答 1

2

您应该为各自的路由创建一个controller并在那里定义一个计算属性,该属性绑定在 上content,每次内容更改时都会触发 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 表示。

请参阅此处进行演示。

希望能帮助到你。

于 2013-08-08T21:05:38.450 回答