[Angualrjs Ng-redux] Map state and Dispatchers to Redux

Source: Internet
Author: User

In this lesson, we is going to learn how to map our Angular component directly to our application store using the Connect method on Ngredux.

In Angular, it's a common technique to bind a controller property directly to the model so that we controllers remain Li Ghtweight and operate more as a pass through mechanism than anything else.

Using The Connect method, we can accomplish the same effect by not only binding properties directly to the application Sto Re, but also binds our controller to our action creators. This allows us-drastically simplify our controllers as we is able to delete a bunch of local controller methods as the Y become unnecessary because of our mappings.

Connect (mapstatetothis, actions) (context):

P1:mapStateToThis:function (state),

P2:actions:object-Actionscreators

P3:context

Basiclly Connect do things:

1. Exports the actions to the controller, so you don ' t need to do like this:

this. Store.dispatch (this. Categoriesactions.getcategoreis ());

Instead can do:

this. Getcategoreis ();

2. You don't need to subscribe to the store manully anymore, it'll automaticlly subscribe to store:

So instead doing like this:

 This this. Store.subscribe (() = {      this-this. Store.getstate (). Categories ;        This  This . Store.getstate (). Category;    });

Now you can does this:

 This this. Store.connect (the. Mapstatetothis, Actions) (this); 

The Connect function also return a unsubscribe function which you can call in $onDestroy life cycle.

mapstatetothis:

  Mapstatetothis (state) {    return  {      categories: state.  Categories,      currentcategory: state.  Currentcategory    };  }

Basiclly it would replace:

 This this. Store.subscribe (() = {      this.  Categoriesthis. Store.getstate ().  Categories;       this. currentcategory  This . Store.getstate (). category;    });

Actions:

Actions  This  This . bookmarksactions);      This this. Store.connect (the. Mapstatetothis, Actions) (this); 

The actions are an object contains the actions creators your need for this controller.

Other Benefits:

So for example you has a function called 'deletebookmark' on the controller. And inside this function, your call ' this. Bookmarksactions. Deletebookmark (bookmark) ' function.

Functions ' name is the same, then you can get rid of the ' deletebookmark ' the whole function.

<type= "button"  class= "Close"  ng-click= " Bookmarkslistctrl.deletebookmark (Bookmark) ">&times; </ Button >
/* //The whole function can Removeddeletebookmark (bookmark) {    This.store.dispatch (this      . Bookmarksactions.deletebookmark (bookmark)    )  }* /

becasue connect function already exprots the 'deletedbookmark' function to the CTRL.

[Angualrjs Ng-redux] Map state and Dispatchers to Redux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.