16

I am using Knockout.js to build a client-side view model. In my view model I would like to expose some functions that can be bound to elements in the page (typical MVVM model). I only want these functions to be called in response to a click event from a button, however they are been called when the view model is been constructed...

I have defined my model like this:

<script type="text/javascript">
var ViewModel = function(initialData) {
    var self = this;

    self.id = initialData;
    self.isSubscribed = ko.observable(false);
    self.name = ko.observable();

    self.SubscribeToCategory = function () {
        $.ajax({
            url: '@Url.Action("Subscribe", "Category")',
            type: 'POST',
            data: {
                categoryId: self.id
            },
            success: function () {
                self.isSubscribed(true);
            },
            failure: function () {
                self.isSubscribed(false);
            }
        });

        alert('Subscribing...');
    };

    self.UnsubscribeFromCategory = function () {
        $.ajax({
            url: '@Url.Action("Unsubscribe", "Category")',
            type: 'POST',
            data: {
                categoryId: self.id
            },
            success: function () {
                self.isSubscribed(false);
            },
            failure: function () {
                self.isSubscribed(true);
            }

        }); 

        alert('Unsubscribing...');
    };

    self.LoadCategory = function () {
        $.ajax({
            url: '@Url.Action("GetCategory", "Category")',
            type: 'POST',
            async: true,
            data: {
                categoryId: self.id
            },
            dataType: 'json',
            success: function (data) {
                self.isSubscribed(data.IsSubscribed);
                self.name(data.Name);
            }
        });
    };

    self.LoadCategory();
};


$(document).ready(function () {
    var data = '@Model';
    var viewModel = new ViewModel(data);
    ko.applyBindings(viewModel);
});

When I execute the code however, the two alerts fire automatically, but I am not expecting them to. I am using ASP MVC4, and the HTML that is using the view model is below:

<p>
    ID: <span data-bind="text: id"></span>
</p>
<div id="subscribe" data-bind="visible: isSubscribed == false">
    <button data-bind="click: SubscribeToCategory()">Subscribe</button>
</div>
<div id="unsubscribe" data-bind="visible: isSubscribed == true">
    <button data-bind="click: UnsubscribeFromCategory()">Unsubscribe</button>
</div>
<div>
    Category Name: <span data-bind="text: name"></span>
    Is Subscribed: <span data-bind="text: isSubscribed"></span>
</div>

I've looked through the tutorials online and some other knockout samples, as well as other places in my code where I have used knockout, but I cannot see why the two functions (SubscribeToCategory and UnsubscribeFromCategory) are firing on document load.

4

2 に答える 2

22

jsfiddle

1秒かかりましたが、簡単な修正になりました。data-bind = "click:SubscribeToCategory()"から()を削除し、両方のクリックハンドラーをこのdata-bind = "click:SubscribeToCategory"とdata-bind = "click:UnsubscribeFromCategory"にします。

于 2012-04-26T12:15:56.857 に答える
5

バインディングの関数名の角かっこが<button data-bind="click: SubscribeToCategory()">Subscribe</button>問題のようです。

()はそこにあるべきではありません。したがって、次のようになります。

<button data-bind="click: SubscribeToCategory">Subscribe</button>
于 2012-04-26T12:16:31.277 に答える