mercredi 31 janvier 2018

Initializing the ModelView in AngularJs

I made a oriented object analysis for some project using a Model View Controller architecture style (Model-View-Presenter). For instance for a given Use Case some View need to ask some controller about some data and then the controller get the data from some models classes. Now for the design we need to use AngularJs (Model-View-ViewModel).

Now some HTML view, for displaying the data, it get the data using the controller (a .js file) via attributes of this controller. This way:

HTML:

....
<div>
    <p></p>
</div> 
....

JS:

....
appControllers.controller ("SomeController",function(){
    var self = this;
    self.someAttribute = 'someValue';
});
....

I think this is the right way to work with AngularJS right? Also the View is associated with the controller via Angular Routing. The issue is the attributes data of the controller (the ModelView) is retrieved via API REST, and I dont know if the right thing to do is to have a private function in the controller getting this data and then call this function when initizalizing the controller:

....
appControllers.controller ("SomeController",function(){
    var self = this;

    getSomeData();       


    function getSomeData  () {      
    $http({
        method : "GET",
        url : someRestResourceURL,  
        headers : {
            'Content-Type' : 'application/json'
        }
    }).then(function successCallback(response) {
        self.someAttribute = angular.fromJson(response.data);

    }, function errorCallback(response) {
    });
}

});




Aucun commentaire:

Enregistrer un commentaire