How to do the @Override of a function in AngularJs?

0

I have an abstract controller with angularjs.

In it I have, among other things, a search toolbar.

I would like to use this search input on all controllers inheriting from the parent controller. being triggered by an "ng-change".

function IndexCtrl() {
  var vm = this;
  //Objects
  vm.searchText = '';

  //Methods
  vm.search = search;

  function search(search) {

  }
}

function TesteCtrl() {
    var vm = this;

    //Methods
    vm.search = search;

    function search(search) {
      console.log(search);
    }
}

What implementation alternatives do I have, when ng-change of the parent controller is fired, the child controller method fires (when in use)?

    
asked by anonymous 07.01.2017 / 12:32

1 answer

0

An alternative to solve your problem would be to use overhead.

Ex: Overload - Number of parameters

Once again, I'll use Js's "apply" method to solve this, because the language messes a lot with prototypes and builders, nothing smarter than using this scope method. The idea I had was to use a function as a function and at the same time as an object that holds its own properties.

var Person = {
Class: function(){  
    //Private
    var name = ''

    //Public
    this.name = function(){
        return this.name[ arguments.length ].apply( this, arguments )
    }

    this.name[0] = function(){ return name }
    this.name[1] = function(n){ name = n }
    this.name[2] = function(a, b){ name = a + " " + b }
    }   
}

Here I create a function named "name" and create 3 more properties of it: 0, 1 and 2. They are numbers, but still are properties of the name method. What the main function will do is to see how many parameters it has and will call its corresponding property, which is nothing other than another method that will be executed in the same context as the name and will have the same parameters as the name. >

Logo:

var Edu  = new Person.Class

Edu .name() //  ''
Edu .name('Eduardo') 
Edu . name() // "Eduardo"
Edu . name('Eduardo', 'Ottaviani')
Edu . name() // "Eduardo Ottaviani"

It's not because I had this idea, but I consider this the most elegant form of operator overload so far. Maybe it was not the most efficient, I did not test its speed in relation to the other techniques. If it is slower, I do not think it is noticeable.

Source: link

    
09.01.2017 / 12:17