2013-08-08 6 views
0

DataPrid의 외부 플러그인을 사용하고 있습니다. 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 함수를 호출하고 데이터를이 함수의 인수로 전달하고 있습니다. 내가 무엇입니까 return this.get('controller.model')를 사용하는 경우

내가 클래스를 얻고 Grid.ModalModel.find()를 사용하여 데이터에 액세스하려고하는 경우는 (CONSOLE.LOG에서 다음 데이터를 참조)

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

:

나는 다음과 같은 문제를 얻고있다

: 나는 다음과 같은 오류를 얻고 객체를 JSON으로 Grid.ModalModel.find() 변환하려고 undefined

Uncaught TypeError: Converting circular structure to JSON 

json 객체 형식으로 데이터를 가져 오기 위해 여기 모델에 액세스하는 방법을 알려 줄 사람이 있습니까?

내가 Uncaught TypeError: Object [object Object] has no method 'toJSON'

+0

어떻게 Grid.ModalModel.find()를 JSON으로 변환하려고합니까? – intuitivepixel

답변

2

당신은 각각의 경로에 대한 controller를 작성하고 content에 결합하는 계산 된 특성이 정의해야

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('[email protected]'), 
     showmodal: function(){  
      $('#modal').modal(); 
    } 
}); 

하지만 console.log(item.toJSON({includeId:true})); 던지는 다음과 같은 오류 코드 다음 사용하려고의 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('[email protected]') 
}); 

는 그런 다음 레코드 JSON 표현을 얻기 위해 DS.Model 내장 방법 toJSON()를 사용합니다.

데모는 here을 참조하십시오.

희망이 있습니다.

+1

좋은 답변 @intuitivepixel. 나는'toJSON'에 대해서 알지 못했고, 콘텐트를 관측하는데 성공하지 못했다. 아마 내가 컨트롤러 대신 라우트에서 관측했기 때문일 것이다. '모델을 관찰하면서 한 가지 조언 만하면됩니다.isUpdating'은 'content. @ each'보다 덜 자주 트리거됩니다. –

+0

@ MárcioRodriguesCorreaJúnior, 조언 해 주셔서 감사합니다.하지만 경로 또는 컨트롤러에서'model.isUpdating'을 관찰 하시겠습니까? – intuitivepixel

+0

컨트롤러에서와 마찬가지로 수행했습니다. 'observes ('content. @ each')'대신'.observes ('model.isUpdating')'을 사용하십시오. 그리고 메소드의 시작에서'if (this.get ('model.isUpdating')) {return; }', while을 실행 중이다. –

관련 문제