KnockOutJs 对象不更新 UI

发布于 2024-12-15 10:13:30 字数 4391 浏览 1 评论 0原文

我正在尝试使用 Ajax 调用更新 KnockOutJS 页面。 Update 函数应从 Web 服务检索 Gifts Observable 数组。这不应该更新模型,然后更新用户界面吗?我一定想念这是如何工作的。我的想法?

谢谢

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <tit
le>Index</title>
    <link rel="Stylesheet" href="/Content/Site.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script>

    <script type="text/javascript" src="/Scripts/knockout-1.2.1.js"></script>
    <script type="text/javascript" src="/Scripts/knockout.mapping.js"></script>
    <script type="text/javascript" src="/Scripts/json2.js"></script>
    <script type="text/javascript" src="http://knockoutjs.com/examples/resources/sampleProductCategories.js">
</script>
</head>
<body>
    <div>
    <h1>Gift list editor 2</h1>
    11/15/2011 12:55:32 PM <br />

    <p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p>

    <form class="giftListEditor" action="" >
        <table style="width: 800px;"> 
            <tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody> 
        </table>


        <button data-bind="click: addGift">Add Gift</button>
        <button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
        <button data-bind="click: update, enable: gifts().length > 0">Partial Update</button>

    </form>

    <script type="text/html" id="giftRowTemplate"> 
        <tr> 
            <td style="width:100px;" >
                <label data-bind="visible: GiftId == 0">New</label>
                <label data-bind="text: 'GiftId: ' + GiftId, visible: GiftId > 0" />
            </td> 
            <td><select data-bind='options: sampleProductCategories, optionsText: "name"' /></td>
            <td>Gift name: <input class="required" data-bind="value: Title, uniqueName: true"/></td> 
            <td>Price: $ <input class="required number" data-bind="value: Price, uniqueName: true" /></td> 
            <td><a href="#" data-bind="click: function() { viewModel.removeGift($data) }">Delete</a></td> 
        </tr>
    </script>

    <script type="text/javascript">

        var initialData =  [{"GiftId":13,"Title":"test","Price":3},{"GiftId":15,"Title":"asdasd","Price":444},{"GiftId":44,"Title":"asd","Price":1},{"GiftId":45,"Title":"asdasd","Price":1231},{"GiftId":46,"Title":"qwe","Price":132},{"GiftId":47,"Title":"eeee","Price":123123},{"GiftId":48,"Title":"www","Price":121212}];    //var initialData = [{ GiftId: "","Title": "Tall Hat", "Price": 49.95 }, { GiftId: "","Title": "Long Cloak", "Price": 78.25}];

        var viewModel = {

            gifts: ko.observableArray(initialData),

            addGift: function () { 
                this.gifts.push({ GiftId: "0", Title: "", Price: "" }); 
            },

            removeGift: function (gift) { 
                this.gifts.remove(gift); 
            },

            update: function () { 
                 $.ajax({
                    url: "/Home/PartialUpdate", 

                    success: function(result){
                        this.gifts =  ko.utils.parseJson(result); //[{ GiftId: "33", Title: "ThirtyThree", Price: "10.50" }] 
                        alert(this.gifts.length);
                    },
                    error:function(xhr,err){ 
                        alert("readyState: " + xhr.readyState+"\nstatus: "+xhr.status); 
                        alert("responseText: " + xhr.responseText);
                    }
                 });
            },

            save: function() { 
                ko.utils.postJson(location.href, { gifts: this.gifts }); 
            } 
        };

        ko.applyBindings(viewModel, document.body);

        $("form").validate({ submitHandler: function() { viewModel.save() } });

    </script>


    </div>
</body>
</html>

I am trying to update a KnockOutJS page using an Ajax call. The Update function should retrieve the Gifts Observable array from a web service. Shouldn't this update the model, then the UI. I must be missing how this works. I ideas?

Thanks

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <tit
le>Index</title>
    <link rel="Stylesheet" href="/Content/Site.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script>

    <script type="text/javascript" src="/Scripts/knockout-1.2.1.js"></script>
    <script type="text/javascript" src="/Scripts/knockout.mapping.js"></script>
    <script type="text/javascript" src="/Scripts/json2.js"></script>
    <script type="text/javascript" src="http://knockoutjs.com/examples/resources/sampleProductCategories.js">
</script>
</head>
<body>
    <div>
    <h1>Gift list editor 2</h1>
    11/15/2011 12:55:32 PM <br />

    <p>You have asked for <span data-bind="text: gifts().length"> </span> gift(s)</p>

    <form class="giftListEditor" action="" >
        <table style="width: 800px;"> 
            <tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody> 
        </table>


        <button data-bind="click: addGift">Add Gift</button>
        <button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
        <button data-bind="click: update, enable: gifts().length > 0">Partial Update</button>

    </form>

    <script type="text/html" id="giftRowTemplate"> 
        <tr> 
            <td style="width:100px;" >
                <label data-bind="visible: GiftId == 0">New</label>
                <label data-bind="text: 'GiftId: ' + GiftId, visible: GiftId > 0" />
            </td> 
            <td><select data-bind='options: sampleProductCategories, optionsText: "name"' /></td>
            <td>Gift name: <input class="required" data-bind="value: Title, uniqueName: true"/></td> 
            <td>Price: $ <input class="required number" data-bind="value: Price, uniqueName: true" /></td> 
            <td><a href="#" data-bind="click: function() { viewModel.removeGift($data) }">Delete</a></td> 
        </tr>
    </script>

    <script type="text/javascript">

        var initialData =  [{"GiftId":13,"Title":"test","Price":3},{"GiftId":15,"Title":"asdasd","Price":444},{"GiftId":44,"Title":"asd","Price":1},{"GiftId":45,"Title":"asdasd","Price":1231},{"GiftId":46,"Title":"qwe","Price":132},{"GiftId":47,"Title":"eeee","Price":123123},{"GiftId":48,"Title":"www","Price":121212}];    //var initialData = [{ GiftId: "","Title": "Tall Hat", "Price": 49.95 }, { GiftId: "","Title": "Long Cloak", "Price": 78.25}];

        var viewModel = {

            gifts: ko.observableArray(initialData),

            addGift: function () { 
                this.gifts.push({ GiftId: "0", Title: "", Price: "" }); 
            },

            removeGift: function (gift) { 
                this.gifts.remove(gift); 
            },

            update: function () { 
                 $.ajax({
                    url: "/Home/PartialUpdate", 

                    success: function(result){
                        this.gifts =  ko.utils.parseJson(result); //[{ GiftId: "33", Title: "ThirtyThree", Price: "10.50" }] 
                        alert(this.gifts.length);
                    },
                    error:function(xhr,err){ 
                        alert("readyState: " + xhr.readyState+"\nstatus: "+xhr.status); 
                        alert("responseText: " + xhr.responseText);
                    }
                 });
            },

            save: function() { 
                ko.utils.postJson(location.href, { gifts: this.gifts }); 
            } 
        };

        ko.applyBindings(viewModel, document.body);

        $("form").validate({ submitHandler: function() { viewModel.save() } });

    </script>


    </div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-12-22 10:13:30

this.gifts 是一个 observableArray。当您设置更新的值时,您需要将其设置为可观察的:

this.gifts(ko.utils.parseJson(result));

您当前的代码正在将gift设置为等于不可观察或当前绑定到UI的新数组。

您可能还需要小心成功函数中的 this。如果 this 不合适,那么您可以选择将 context: viewModel 添加到 $.ajax 选项中。

this.gifts is an observableArray. When you set the updated value, you need to set it as an observable:

this.gifts(ko.utils.parseJson(result));

The current code that you have is setting gifts equal to a new array that is not observable or currently bound against the UI.

You may also need to be careful with this in your success function. If this is not appropriate, then you could choose to add a context: viewModel to your $.ajax options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文