为什么我的KO绑定需要括号? [英] Why is my KO bindings need parentheses?

查看:155
本文介绍了为什么我的KO绑定需要括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用迪朗达尔/基因剔除/微风/的WebAPI与MVC4作为后端的网站。
我通过查询清风我的API,像这样:

I'm developing a website using Durandal/Knockout/Breeze/WebApi with MVC4 as the back end. I'm querying my api via breeze like so:

var getCategories = function() {
    var query =
        entityQuery
            .from('Categories')
            .orderBy('Order');

    return manager.executeQuery(query);
};

然后,在我的视图模型:

Then, on my view model:

function initCategories() {
        service.getCategories()
               .then(querySuccess)
               .fail(queryFail);

        function querySuccess(data) {               
            vm.categories(data.results);
        };

其中VM是我的有界视图模型和类别是当然的 observableArray 的的。

where vm is my bounded view model and categories is of observableArray of course.

最后,我的观点有:

<!-- ko foreach: categories -->

<div class="list_images">
    <a data-bind="attr: { href: '#search/' + queryString() }" class="hover-shadow">
        <img data-bind="attr: { src: image(), alt: name() }" width="240" height="180">
        <h5 data-bind="text: name()"></h5>
    </a>
</div>

<!-- /ko -->

下面是一个什么样data.results截图包括:

Here's the screenshot of what data.results contains:

它工作正常,除了需要使用括号。
与正常的ViewModels我不需要在视图中绑定的括号。
我想不通为什么它与微风对象(实体)才会发生。

It works fine, except for the need of using the parentheses. With 'normal' viewmodels I don't need parentheses in the view bindings. I can't figure out why it happens only with breeze objects (Entities).

修改
在进一步调查之后我发现我的实体类型的 proto._setCtor.proto ,而不是仅仅一个对象。为什么?
即使我用微风管理器来创建一个新的实体 - 这是我回来的对象:(
 什么是错在这里?

Edit After further investigation I noticed that my entities are of type proto._setCtor.proto instead of just an Object. Why's that? Even if I use the breeze manager to create a new entity - this is the object I get back :( What's wrong here?

推荐答案

这是不是一个答案。这是我大惑不解忏悔。的我不能复制您所描述的问题。

This isn't an answer. This is a confession that I'm mystified. I can't duplicate the problem you describe.

我明白你问的是什么。我同意,你不应该使用括号。你应该能够写:

I understand exactly what you are asking. I agree that you should not have to use the parentheses. You should be able to write:

<h5 data-bind="text: name"></h5>

和不必写:

<h5 data-bind="text: name()"></h5>

我从网上下载的微风藤堂敲除的样本。确认它的工作后,我开始改变它看起来更像是你的绑定的例子。我继续工作。

I downloaded the "Todo-Knockout" sample from Breeze. After confirming that it worked, I started changing it to look more like your binding example. I continued to work.

您可以和我一起跟着,一步一步,确认一切正常,每个步骤后预期。

You can follow along with me, step-by-step, confirming that everything works as expected after each step.


  • 切换到中继器的评论表单:&LT; - 阁的foreach:项目 - &GT;

取代了&LT; UL&GT; &LT;李&GT; 与标记 DIV 容器中。

Replaced the <ul> and <li> tags with div container.

切换到KO的调试版本(这是你使用的是什么)

Switched to the debug version of KO (that's what you're using)

更​​新到最新的KO(淘汰赛3.1.0.debug.js 的)

Updated to the latest KO (knockout-3.1.0.debug.js)

在最后,我的修订标记看起来是这样的:

In the end, my revised markup looks like this:

<!-- ko foreach: items -->
<div>
    <div data-bind="visible: !isEditing()">
        <input type="checkbox" data-bind="checked: IsDone" />
        <label data-bind="text: Description, click: $parent.editBegin, css: { done: IsDone, archived: IsArchived }"></label>
        <a href="#" data-bind="click: $parent.deleteItem">X</a> 
    </div>
    <div data-bind="visible: isEditing">
        <form data-bind="event: { submit: $parent.editEnd }">
            <input type="text" data-bind="value: Description, hasfocus: isEditing" />
        </form>
    </div>
</div>
<!-- /ko -->

当我在 Chrome开发者工具那里的结果是从控制台服务器和显示器 data.results 回破,我得到这样的:

When I break in the Chrome Developer Tools where the results are returned from the server and display data.results in the console, I get this:

[proto._setCtor.proto
    CreatedAt: function dependentObservable() {
    Description: function dependentObservable() {
    Id: function dependentObservable() {
    IsArchived: function dependentObservable() {
    IsDone: function dependentObservable() {
    entityAspect: ctor
    isEditing: function observable() {
    __proto__: Object

, proto._setCtor.proto, proto._setCtor.proto]

我没有看到你从任何为例差异显著。你做?

I'm not seeing any significant differences from your example. Do you?

当你做同样的藤堂敲除样品同样的事情,你的机器上会发生什么?

What happens when you do the same thing with the same "Todo-Knockout" sample on your machine?

是什么浏览器您使用?你看到同样的不当行为在Chrome?

What browser are you using? Do you see the same misbehavior in Chrome?

这篇关于为什么我的KO绑定需要括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆