2011-11-06 180 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

工作尝试删除“自动加载:真正的”从你的代码。它解决了我和你们一样的问题。