2011-11-06 105 views
1

型號:煎茶觸摸:JSONP解析

app.models.Category = Ext.regModel("Category", { 
    fields: [ 
     { name: 'CategoryId', type: 'int' }, 
     { name: 'ImageUrl', type: 'string' }, 
     { name: 'ImageUrlFile', type: 'string' }, 
     { name: 'CategoyName', type: 'string' } 
    ] 
}); 

存儲:

app.stores.CategoryStore = new Ext.data.Store({ 
    id: 'CategoryStore', 
    model: 'Category', 
    autoLoad: true, 
    proxy: { 
     type: 'scripttag', 
     url: 'http://localhost:1303/admin/categoriesservice/getcategories', 
     mehod: 'GET', //not needed 
     callbackKey: 'callback', //not needed 
     reader: { 
      type: 'json', 
      root: 'categories'//not needed with my JSONP 
     }, 
     afterRequest: function (request, success) { 
      console.log("afterRequest"); 
      if (success) { 
       console.log("success"); 
      } else { 
       console.log("failed"); 
      } 
      console.log(request); 
     } 
    } 
}); 

控制器:

Ext.regController('Home', { 
    index: function() { 
     if (!this.indexView) { 
      this.indexView = this.render({ 
       xtype: 'HomeIndex' 
      }); 
      this.items = [app.views.HomeIndex]; 
     } 
     app.viewport.setActiveItem(this.indexView);//this what i've missed 
    } 
}); 

查看

app.views.HomeIndex = Ext.extend(Ext.DataView, { 
    html: '<div class="gallery-view" style="display: block;width: 300px;border: 1px solid #fff;height: 300px;"></div>', 
    store: app.stores.CategoryStore, //full namespace needed 
    itemSelector: 'div.node', 
    initComponent: function() { 
     this.tpl = new Ext.XTemplate(
        '<div style="padding:10px 5px 5px 5px;">', 
         '<tpl for=".">', 
          '<div class="node" style="background:url({ImageUrl});">', 
          '</div>', 
         '</tpl>', 
        '</div>' 
     ); 
    //appened to successful solution 
    this.dataView = new Ext.DataView({ 
     store: this.store, 
     tpl: this.xtpl, 
     itemSelector: 'div.node' 
    }); 
    this.items = [this.dataView]; 
     app.views.HomeIndex.superclass.initComponent.apply(this, arguments); 
    } 
}); 
Ext.reg('HomeIndex', app.views.HomeIndex); 

JSONP結果:

GetCategories([{"CategoryId":101,"CategoyName":"אוכל","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/rest.png","ImageUrlFile":null,"InsertDate":"\/Date(1314507534000)\/","IsActive":true,"ApplicationId":0,"Applications":null},{"CategoryId":99,"CategoyName":"הצגות ומופעים","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/shows.png","ImageUrlFile":null,"InsertDate":"\/Date(1314442037000)\/","IsActive":true,"ApplicationId":100,"Applications":null},{"CategoryId":111,"CategoyName":"בריאות","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/spa.png","ImageUrlFile":null,"InsertDate":"\/Date(1314856845000)\/","IsActive":true,"ApplicationId":0,"Applications":null},{"CategoryId":142,"CategoyName":"נופש ותיירות","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/vacation.png","ImageUrlFile":null,"InsertDate":"\/Date(1314713031000)\/","IsActive":true,"ApplicationId":0,"Applications":null},{"CategoryId":143,"CategoyName":"ביגוד","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/clothes.png","ImageUrlFile":null,"InsertDate":"\/Date(1314713031000)\/","IsActive":true,"ApplicationId":0,"Applications":null},{"CategoryId":144,"CategoyName":"אתרים ואטרקציות","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/attraction.png","ImageUrlFile":null,"InsertDate":"\/Date(1314713031000)\/","IsActive":true,"ApplicationId":0,"Applications":null},{"CategoryId":105,"CategoyName":"חשמל","ImageUrl":"http://www.teleclal.com/YnetApplicationMall/Content/images/categories/elctronic.png","ImageUrlFile":null,"InsertDate":"\/Date(1314713031000)\/","IsActive":true,"ApplicationId":0,"Applications":null}]); 

例外: 遺漏的類型錯誤:無法讀取未定義

問題的特性 '長度': 幫助我如何通過存儲或任何其他方式解析JSONP問題?

回答

0

當我試圖限定

function GetCategories(data){console.log(data);} 

,然後調用JSONP響應(通過CTRL + C/CTRL + V到控制檯),I是成功的,這意味着所述數據是有效的JSON字符串。
但是當我檢查你的代碼時,我還沒有看到定義該函數的機制(GetCategories)。

我必須說我對sencha並不熱愛。但我擔心這個問題是沒有創建回調函數。

我看到你定義了一個'callbackKey:'回調''
有沒有在sencha文檔中的某處,允許你定義一個'callbackValue:'GetCategories''或者類似的東西?試着去檢查那個方向。

+0

我知道數據被接收,因爲我在調試看到它,但存儲和JSONP是那些獲得不工作,因爲我看不到在模板中的任何結果之間的結合,我認爲這是一些別的東西, BTW:自動加載:真調用結果 – IamStalker

1

你已經在你的讀者

reader: { 
      type: 'json', 
      root: 'categories' 
     } 

設置這一點,我看不到類別元素在你的JSON數據。檢查,如果這是正確的還是這是爲了添加到您的JSON將可能

{"categories":[ ...//old json //..]} 
+0

不,我發現錯誤,它的控制器丟失: app.viewport.setActiveItem(this.indexView); – IamStalker

0

工作嘗試刪除「自動加載:真正的」從你的代碼。它解決了我和你們一樣的問題。