--
self.User(new User(data.Id, data.Name))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> <!--jQuery References--> <script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script> <script src="http://knockoutjs.com/downloads/knockout-2.2.0.debug.js"></script> |
|
|
|
|
|
|
</head> <body> @RenderBody() </body> </html> |
1 2 3 4 5 6 7 | @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <span data-bind="text: User"></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace KnockoutTests.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } public JsonResult User(int id) { return Json(new User { Id = "users/1", Name = "Stacey", Points = new Random(DateTime.UtcNow.Millisecond).Next() }, JsonRequestBehavior.AllowGet); } } } |
1 2 3 4 5 6 7 8 9 10 | using System; namespace KnockoutTests { public class User { public string Id { get; set; } public string Name { get; set; } public int Points { get; set; } } } |
callback as a function on the viewModel doesn't seem to work (I am not sure why) but declaring an inline function does yield ... something different. Still isn't working though. Here is an update.
this.User = ko.onDemandObservable(function(){
$.ajax({
url: '/home/user/',
data: { id: 1 },
dataType: 'json',
success: function (data) {
self.User(new User(data.Id, data.Name));
}
});
}, this);
//this.Update = function () {
// $.ajax({
// url: '/home/user/',
// data: { id: 1 },
// dataType: 'json',
// success: function (data) {
// self.User(new User(data.Id, data.Name));
// }
// });
//};
};
var model = new viewModel();
ko.applyBindings(model);
model.User();
});
</script>
And then trying to display any of the data retrieved still fails.
<span data-bind="text: User.Name"></span>
A bit of a breakthrough! If I change the declarative binding to look like this ..
<span data-bind="with: User">
<span data-bind="text: Id"></span>
<span data-bind="text: Name"></span>
</span>
Then I am starting to see results. I think I am almost getting there...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> <!--jQuery References--> <script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script> |
|
|
|
|
|
|
|
|
</head> <body> @RenderBody() </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 | |
<span data-bind="with: User"> <span data-bind="text: Id"></span> <span data-bind="text: Name"></span> |
|
</span> <button data-bind="click: Refresh">Refresh</button> |
--
--
function viewModel() {
var self = this;
this.User = ko.observable();
this.GetUser = function() {
$.ajax({
url: '/home/user/',
data: {
id: 1
},
dataType: 'json',
success: function(data) {
self.User(new User(data.Id, data.Name, data.Points));
}
});
};
this.Refresh = this.GetUser; //well, yeah, not really necessary...
this.GetUser(); //initial call to populate it directly on instantiation. Could be triggered from something else.
};
--
--
--
--
--
I understand it fine. Not all programs work the same way and sometimes you just have to do things that were not intended by the original framework.
--
--
--
@Dave
That's a pretty narrow minded view, and a rather hateful response to the specific question. You've no idea what is being asked beyond the scope of a very limited question that was really aimed at nothing more than getting data to an observable after the dom has loaded, and the root of the problem turned out to be different than believed at first.
Seriously, just because it isn't the way you do it doesn't mean trying it is wrong.
Having a problem with one aspect of a framework doesn't mean I don't understand the concepts, and it certainly doesn't mean that the way you've done it up until now is the only way.
Ease up on the hateraide a bit.
--
Also, my 'understanding' of MVVM isn't really the question.
I had a project, I needed to accomplish something. The things I were trying were not working, so I came to other people who work with the same framework and might have encountered the same problem.
I never asked for an 'on demand observable'. I was directed to one. It was the best lead I had so I followed it.
Isn't that kind of how the debugging process works?
--
I am simply trying to pull some data from an ajax request. The ajax call works - I know the data is retrieved. But it just isn't setting the value of the ko.computed...
function viewModel() {this.Id = ko.observable("@Model.Identity()");
this.Points = ko.computed({read: function () {
$.ajax({url: '/skills/points',data: { id: this.Id },type: 'get',success: function (data) {return data;}});},owner: this});}
So a call like ...
<span data-bind="text: Points"></span>
Yes. Thank you all very much for the answers. It is good to see the knockout community still has so many helpful people. I was actually considering a switch to kendo's mvvm framework but the fact that they don't have people like you guys dissuaded me to stay with knockout.
The solution to this problem was several fold, and since I don't want to cause any trouble, I'll try to clarify everything here.
First. I wanted to get a value from a URL because I only needed knockout for a very specific part of the page, and was already passing down a REALLY big view model from the MVC controller. I thought a simple ajax request was less invasive, because the things I needed were not on the original view model.
Second, the on demand observable was actually suggested to me on stack overflow.
Third, party of the problem kept coming with the fact that updating the VIEW MODEL was not updating the actual DOM. this is where the biggest disconnect came from and why I kept assuming the suggestions were not working. I will be posting a follow-up with more detail soon on a different thread.
And fifth, outright curiosity. Once I had made it do one thing, I wanted to see how far I could push the idea just for the sake of exploring it.
--
I don't understand why the async stuff even matters. I tell it to bind to a specific observable - and then I update that observable, shouldn't it change? Isn't that the entire point of what knockout is supposed to do?
On Friday, December 14, 2012 7:20:27 PM UTC-6, Stacey wrote:You said that you can't see what would trigger it except being called directly. Shouldn't being data bound be the same thing? Saying
<span data-bind="text: Points"></span>
call it directly and cause it to evaluate? I really don't understand why it isn't that simple. All of the async stuff I keep looking at makes little to no sense to me.
--