-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] React Angular?

, 26 2017 . 19:19 +

Akili javascript , React, Angular, Aurelia . , .

React, JSX? Angular, ?

.

, - . . , Babel (es2015 + stage-0).


import Akili from 'akili';

class MyComponent extends Akili.Component {
  constructor(el, scope) {
    super(el, scope);
    scope.example = 'Hello World';
  }
}

Akili.component('my-component', MyComponent); //  

document.addEventListener('DOMContentLoaded', () => {
  Akili.init(); //  
});


  ${ this.example }


, . , .

-, . , .

class MySecondComponent extends MyComponent  {
 constructor(...args) {
    super(...args);
    this.scope.example = 'Goodbye World';
  }
  myOwnMethod() {}
}

Akili.component('my-second-component', MySecondComponent)


  ${ this.example }
  ${ this.example }


scope. , ${ this.example }, this scope. javascript .

-, . scope :

class MyComponent extends Akili.Component {
  constructor(el, scope) {
    super(el, scope);
    scope.example = 'Hello World';
    scope.test = 'Test';
  }
}

:


  
     ${ this.example }
     ${ this.example } - ${ this.test }
   


:


  
     Hello World
     Goodbye World - Test
   


-, scope .

class MyComponent extends Akili.Component {
  constructor(...args) {
    super(...args);
    this.scope.example = 'Hello World';

    setTimeout(() => {
      this.scope.example = 'Goodbye World';
    }, 1000);
  }
}

.

Lifecycle , React


.constructor(el, scope)
, javascript , . html , scope. , , , .cancel().

.created()
, . . React, componentWillMount.

.compiled()
, .
React componentDidMount. , .

.resolved()
, , .
, Akili , . . , :

class MyComponent extends Akili.Component {
  static templateUrl = '/my-component.html';

  constructor(...args) {
    super(...args);
    this.scope.example = 'Hello World';
  }
}

, :

class MyComponent extends Akili.Component {
  static templateUrl = '/my-component.html';

  constructor(...args) {
    super(...args);
    this.scope.example = 'Hello World';
  }

  compiled() {
     return new Promise((res) => setTimeout(res, 1000));
  }
}

compiled , resolved . .

resolved , , , , - .

.removed()
. componentDidUnmount.

.changed(key, value)
. componentWillReceiveProps.
, .

, ,


, . :

import Akili from 'akili';

class NineComponent extends Akili.Component {
  static template = '${ this.str }';

  static define() {
     Akili.component('nine', NineComponent);
  }
  constructor(...args) {
    super(...args);
    this.scope.str = '';
  } 
  compiled() {
     this.attrs.hasOwnProperty('str') && this.addNine(this.attrs.str);
  }
  changed(key, value) {
     if(key == 'str') {
        this.addNine(value);
     }
  }
  addNine(value) {
    this.scope.str = value + '9';
  }
}

:

import NineComponent from './nine-component';

NineComponent.define();
Akili.component('my-component', MyComponent); 

document.addEventListener('DOMContentLoaded', () => {
  Akili.init();
});


  
     
    


, :


  
    Hello World9
    


, NineComponent . , - - . 9 .

Akili React.
this.attrs => this.props. , :

Akili attrs scope Proxy, , html , - . attrs .

. , this.example MyComponent , changed NineComponent. , . str , .

changed.

class NineComponent extends Akili.Component { 
  changed(key, value) {
     if(key == 'str') {
        this.addNine(value);
     }
  }
}

class NineComponent extends Akili.Component { 
  changedStr(value) {
     this.addNine(value);
  }
}

. , . : changed + .


, on, . :

class MyComponent extends Akili.Component {
  static events = ['timeout'];

  constructor(...args) {
    super(...args);
    this.scope.example = 'HelloWorld';
    this.scope.sayGoodbye = this.sayGoodbye;
  }
  compiled() {
      setTimeout(() => this.attrs.onTimeout.trigger(9), 5000);
  }
  sayGoodbye(event) {
      console.log(event instanceof Event); // true
      this.scope.example = 'Goodbye World';
  }
}


  
    
    ${ this.example }
    


. , .


class MyComponent extends Akili.Component {
  constructor(...args) {
    super(...args);

    this.scope.data = [];

    for (let i = 1; i <= 10; i++) {
      this.scope.data.push({ title: 'value' + i });
    }
  }
}


  
    ${ this.loopIndex } => ${ this.loopKey} => ${ this.loopValue.title  }
  



  
  • ${ this.loopValue }



Akili , ajax , , , .., .

Akili, - , , . , , .

, , )
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331704/

:  

: [1] []
 

:
: 

: ( )

:

  URL