Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  55] [ 7]  / answers: 1 / hits: 21399  / 12 Years ago, thu, april 26, 2012, 12:00:00

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.


More From » function

 Answers
38

jsfiddle



It took me a second, but ended up being a simple fix. remove the () from your data-bind=click: SubscribeToCategory() and make both you click handlers this data-bind=click: SubscribeToCategory and data-bind=click: UnsubscribeFromCategory


[#85958] Wednesday, April 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
feliciarainak

Total Points: 104
Total Questions: 91
Total Answers: 83

Location: Greenland
Member since Thu, Oct 27, 2022
2 Years ago
;