Mik*_*evz 2 extjs ruby-on-rails sencha-touch
我有以下型号
Ext.regModel("Entries", {
fields: [
{name: "id", type: "int"},
{name: "title", type: "string"},
{name: "bought", type: "boolean"},
],
proxy: {
type: 'rest',
url: '/lists',
format: 'json',
reader: {
type: 'json'
}
}
});
Run Code Online (Sandbox Code Playgroud)
然后我有一个列表,从这个模型填充
...
store: new Ext.data.Store({
model: "Entries",
autoLoad: true,
remoteFilter: true
}),
...
Run Code Online (Sandbox Code Playgroud)
列表已正确填充.但是当我尝试执行以下操作时
listeners: {
itemswipe: function (record, index, item, e) {
var el = Ext.get(item);
el.toggleCls("crossedOut");
var store = record.getStore();
var rec = store.getAt(index);
if (el.hasCls('crossedOut')) {
rec.set('bought', true);
rec.save({
success: function() {
console.log("Saved!");
}
});
} else {
console.log('not crossed out');
rec.set('bought', false);
rec.save({
success: function() {
console.log("Saved!");
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
当触发滑动事件时,我遇到以下错误
Uncaught TypeError: Cannot read property 'data' of undefined gsencha-touch.js:6
(anonymous function) sencha-touch.js:6
Ext.data.Connection.Ext.extend.onCompletesencha-touch.js:6
Ext.data.Connection.Ext.extend.onStateChangesencha-touch.js:6
(anonymous function)
Run Code Online (Sandbox Code Playgroud)
我明白,我返回的有效载荷存在一些问题,但是我找不到正确的有效载荷的例子,而且我的所有猜测都不起作用.
在后端我返回以下内容
format.json {render :json => {:data => @list, :success=> true, :id => @list.id}}
Run Code Online (Sandbox Code Playgroud)
我正在使用ExtJS 4预览版,但它应该与Sencha Touch一样.您的问题可能与返回的json的嵌套有关.这对我有用.
在Rails控制器中:
def index
respond_with @entries = Entry.all do |format|
format.json { render :json => {:success => true, :data => @entries, :total => @entries.count} }
end
end
def show
respond_with @entry = Entry.find(params[:id]) do |format|
format.json { render :json => {:success => true, :data => [@entry]} }
end
end
def create
respond_with @entry = Entry.create(params[:records][0]) do |format|
format.json { render :json => {:success => true, :data => [@entry]} }
end
end
def update
@entry = Entry.find(params[:id])
@entry.update_attributes(params[:records][0])
respond_with @entry do |format|
format.json { render :json => {:success => true, :data => [@entry]} }
end
end
Run Code Online (Sandbox Code Playgroud)
ExtJS模型:
Ext.regModel("Entries", {
fields: [
{name: "id", type: "int"},
{name: "title", type: "string"},
{name: "bought", type: "boolean"},
],
proxy: {
type: 'rest',
url: '/lists',
format: 'json',
reader: {
type: 'json',
root: 'data',
record: 'entry'
}
}
});
Run Code Online (Sandbox Code Playgroud)
与你所做的有两点不同:
1 /阅读器的记录选项告诉ExtJS在json中查找嵌套记录.它告诉它寻找:
data: [
{
entry: {
id: 1,
title: "Title 1",
bought: true
}
}
]
Run Code Online (Sandbox Code Playgroud)
代替:
data: [
{
id: 1,
title: "Title 1",
bought: true
}
]
Run Code Online (Sandbox Code Playgroud)
在阅读器上设置记录属性的另一种方法是在Rails中禁用嵌套的json,方法是将其转储到应用程序配置中:
config.active_record.include_root_in_json = true
Run Code Online (Sandbox Code Playgroud)
2 /当返回单个记录时,json输出应该与集合相同,除了你只有一条记录.
Sencha Touch和ExtJS 4文档有时可能有点稀疏,我发现解剖这些例子是最好的学习方法.
HTH
| 归档时间: |
|
| 查看次数: |
2536 次 |
| 最近记录: |