【问题标题】:Titanium Alloy Data Binding: How to get current model from clicked view?钛合金数据绑定:如何从点击视图中获取当前模型?
【发布时间】:2014-10-02 15:01:24
【问题描述】:

我是 Titanium Alloy、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();

【问题讨论】:

    标签: javascript backbone.js titanium titanium-alloy


    【解决方案1】:

    根据 OP 的评论更新答案:

    在您发布的第二个链接上,如果将数据绑定到视图,则表示您需要在视图模板中的所有元素上设置 touchEnabled="false",然后在 alertModel 函数中使用 e.source.model。经过测试,它对我有用。

    var modelId = e.source.model,
            model = issues.get(modelId),
            issueNumber = model.get("issueNo");
    

    我总是使用 ListViews 来绑定我的数据,你不需要在使用时显式设置 touchEnabled。

    此外,alloy_id 在您将模型保存到数据库之前不会被创建。在你的集合上调用 .add() 不会保存它,你必须在你的模型上调用 .save() 才能保存它。

    示例:https://github.com/foolprooflabs/testforcurzmg

    【讨论】:

    • 感谢菲尔的快速回复。也许我没有很好地解释自己。目前的问题是模型没有被传递给控制器​​。我收到错误“'undefined' is not an object (evalating 'model.get')”,我认为这意味着模型没有正确传递。我注意到即使我只是简单地提醒 e.model 然后对话框出现空,所以我认为问题可能与 e.model 错误有关,但我不确定。
    • 好的 np,我更新了答案。答案在您发布的第二个链接中:P
    • 再次感谢 phil,但它似乎仍然无法正常工作。同样,如果我尝试仅提醒 e.source.model 我得到一个空对话框,这似乎导致无法从集合中获取模型。尽管事实上禁用触摸确实有效,因为我已经查看了 e.source。 Titanium什么时候添加alloy_id,也许它是一个订单?如果您仍然拥有它,您能否为我发布您的工作代码,因为我可能会认识到差异。感谢您的帮助。
    • 是的,对不起,我错过了代码中的一个重要点,模型的alloy_id 只有在模型保存到数据库时才会创建(添加到集合中不会将其保存到数据库中)所以在你调用 .add(issue1) (甚至不是)之后,你应该调用 issue1.save()。此处示例项目的 Github 链接:github.com/foolprooflabs/testforcurzmg(我评论时仍在上传)
    • 啊,搞定了!谢谢你,菲尔,你很有帮助。我现在只是将问题标记为已解决。
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多