MooTools request: onSuccess does not fire

2

I'm using KnockoutJS in conjunction with MooTools, but I'm having trouble with 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 initialize is fired, but the onSuccess method is not, <select> is empty. However in the network tab of the development tools you can see that JSON is downloaded. There are no errors in the console.

    
asked by anonymous 15.09.2014 / 15:39

2 answers

2

You have to define the handler of the onSuccess event within the Request Class. The send() method accepts data to pass to the server side and options for the Class, but not new Class Events. Take a look in the documentation and notice the difference between options and events.

Test like this:

App.requests.classificacoes = new Request.JSON({
    url: "/classificacoes.json",
    method: "get",
    onSuccess: function(classificacoes) {
        // this.classificacoes.push(classificacoes); // acho que aqui está a usar o this de maneira incorreta
        alert("baixou");
    }
});

App.viewModels.selectsProcedimentos = new Class({
    initialize: function() {
        this.classificacoes = ko.observableArray([]);
        App.requests.classificacoes.send();
        alert("initialize");
    }
});

With this code above solve the most obvious problem. But I do not understand how you want to use this.classificacoes.push(classificacoes); since ajax is asynchronous.

Would not it be the case to make the request and in onSuccess then instantiate the class App.viewModels.selectsProcedimentos ? or make .push() for the class property that stores this array with App.viewModels.selectsProcedimentos.classificacoes.push(classificacoes);

    
15.09.2014 / 15:44
1

I was able to do this:

Javascript:

App.viewModels.selectsProcedimentos = new Class({
    initialize: function(classificacoes) {
        this.classificacoes = ko.observableArray(classificacoes);
    }
});

App.init.selectProcedimentos = function() {
    new Request.JSON({
        url: "/classificacoes.json",
        method: "get",
        onSuccess: function(classificacoes) {
            ko.applyBindings( new App.viewModels.selectsProcedimentos(classificacoes) );
        }
    }).send();
};

And the HTML:

<div class="form-group">
    <select data-bind="options: classificacoes,
                       optionsText: 'ds_classificacao',
                       optionsValue: 'id',
                       optionsCaption: 'Selecione uma Classificação'"
            class="form-control input-sm"></select>
</div>

<script>
    App.init.selectProcedimentos();
</script>

Thanks for the patience of @Sergio and @Wakim

Now it's breaking the head to make the selects that are dependent on it.

    
15.09.2014 / 18:07