我是鈦合金,MVC框架和Backbone.js的新手,所以我在包裝一些概念時遇到了一些麻煩。鈦合金數據綁定:如何從點擊視圖獲取當前模型?
類似的問題以前已經問:
Getting ID of clicked TableRow in Titanium Alloy?
Appcelerator Titanium Alloy: How to store data on a ScrollableView to use in click event
但是,雖然我試圖按照答案,我似乎無法得到下面的代碼按預期運行。
基本上,當鈦合金中的數據綁定時,您如何將當前模型傳遞給控制器? (另外,這是我在這裏的第一個問題,所以如果有任何方面我可以改進我的查詢請讓我知道,謝謝)。
型號
exports.definition = {
config: {
columns: {
"issueNo": "number",
"date": "string",
"summary": "string",
"cover": "string"
},
adapter: {
type: "sql",
collection_name: "issue"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
});
return Collection;
}
};
查看
<Alloy>
<Collection src="issue" />
<Window class="container" onClose="cleanup">
<View id="header">
<Label id="title">Christian</Label>
</View>
<View id="library" class="body" layout="vertical" dataCollection="issue">
<View id="issuesList" model="{alloy_id}" onClick="alertModel">
<ImageView id="cover" image="{cover}"></ImageView>
<Label id="date" text="{date}"></Label>
<Label id="summary" text="{summary}"></Label>
<View class="border"></View>
</View>
</View>
<View id="store" class="body" layout="vertical" visible="false" backgroundColor="yellow">
</View>
<View id="footer">
<TabbedBar id="bottomNav" platform="ios" index="0" onClick="viewSelect">
<Labels>
<Label>Library</Label>
<Label>Store</Label>
</Labels>
</TabbedBar>
</View>
</Window>
</Alloy>
控制器
var Newsstand = require("ti.newsstand");
Newsstand.enableDevMode();
var issues = Alloy.Collections.issue;
issues.fetch();
var issue1 = Alloy.createModel("issue", {
issueNo: 1,
date: "1/1/2014",
summary: "Some text.",
cover: "/issues/cover_1.png"
});
var issue2 = Alloy.createModel("issue", {
issueNo: 2,
date: "1/2/2014",
summary: "Some text.",
cover: "/issues/cover_2.png"
});
var issue3 = Alloy.createModel("issue", {
issueNo: 3,
date: "1/3/2014",
summary: "Some text.",
cover: "/issues/cover_3.png"
});
issues.add(issue1);
issues.add(issue2);
issues.add(issue3);
function viewSelect(tabbedBar) {
var index = tabbedBar.index,
library = $.library,
store = $.store,
bottomNav = $.bottomNav;
if (index === 0) {
library.visible = true;
store.visible = false;
} else {
store.visible = true;
library.visible = false;
}
}
function alertModel(e){
var modelId = e.model,
model = issues.get(modelId),
issueNumber = model.get("issueNo");
alert(issueNumber);
}
function cleanup() {
$.destroy();
}
$.index.open();
感謝phil爲快速響應。也許我沒有完全解釋自己。目前的問題是該模型沒有被傳遞給控制器。我得到的錯誤「'未定義'不是一個對象(評估'model.get')」,我認爲這意味着模型沒有正確傳遞。我注意到,即使我只是提醒e.model,然後對話框就會變空,所以我認爲這個問題可能與e.model有問題有關,但我不確定。 – curzmg 2014-10-03 16:03:44
好的,我更新了答案。答案在您發佈的第二個鏈接中:P – phil 2014-10-06 00:33:35
再次感謝phil,但它似乎仍然沒有工作。再一次,如果我試圖提醒只是e.source.model我得到一個空的對話框,這似乎導致無法從集合中獲取模型。儘管觸摸禁用功能確實有效,但我已經看過了e.source。鈦何時添加alloy_id,或許它是一個訂單的東西?如果您仍然擁有它,您能否爲我發佈您的工作代碼,因爲我可能會認識到差異。我很感激幫助。 – curzmg 2014-10-06 15:14:11