2
I’m using Knockoutjs in conjunction with Mootools, but I’m having problems with the Request.
<div class="form-group">
<select data-bind="options: classificacoes, optionsText: ds_classificacao, optionsValue: id"></select>
</div>
<script>
ko.applyBindings( new App.viewModels.selectsProcedimentos() );
</script>
App.requests.classificacoes = new Request.JSON({
url: "/classificacoes.json",
method: "get"
});
App.viewModels.selectsProcedimentos = new Class({
initialize: function() {
this.classificacoes = ko.observableArray([]);
App.requests.classificacoes.send({
onSuccess: function(classificacoes) {
this.classificacoes.push(classificacoes);
alert("baixou");
}
});
alert("initialize");
}
});
What happens here is that the initialize
is fired, but the method onSuccess
no, being that the <select>
is empty. However in the tab network of the development tools you can see that the JSON is downloaded. There are no errors in the console.
I’m really having a hard time finding the best way to do this. The point is that there are 4 selects, this is only the first, with the others I will have to pull by AJAX after anyway. I will do the following: I will create the Request within the same class. As soon as it works I update the question with the solution.
– user7261
@Andrey means the Knockout Class within the Request of Mootools and not "Request within the Class", right? If I put these 4 selects in a jsFiddle and post here I can take a look.
– Sergio
I meant to do
new Request({...}).send();
within my viewModel class. I didn’t do Jsfiddle because his AJAX never worked right for me.– user7261
@Andrey, why don’t you
classificacoes
being acomputed
? The moment thecomputed
is "evaluated" byknockout
, you make the request. That’s good because if you use anyobservable
within thecomputed
, by changing the value ofobservable
, thecomputed
is "reevaluated".– Wakim
@Wakim I’m starting with Knockout. How does computed work? I do the AJAX request inside it and return?
ko.computed(function() { return "meu array json" });
? The problem is that the request is asynchronous.– user7261
Yeah, on second thought, it’s not worth it, because you’d have to create a "flag"
observable
to create a dependency and force the "reevaluation" ofcomputed
. This is only worth it if AJAX to recover the values ofclassificacoes
depend on some other value that can change. I think it will not go well in your case.– Wakim