LaxarJS / laxar-patterns

Utilities to implement standard event patterns in LaxarJS widgets
MIT License
5 stars 2 forks source link

actions: allow to auto-bind action publisher to `$scope.actions.myFeature` #66

Closed x1B closed 8 years ago

x1B commented 8 years ago

When writing unidirectional dataflow apps with LaxarJS, it would be handy to have action publishers automatically be registered under axContext.actions (similar to resources, but in the opposite direction).

x1B commented 8 years ago

(draft changelog comment removed)

x1B commented 8 years ago

Implemented on master (v2.0.0) and master-1.x (v1.3.0).

Upgrade Information

There is a new method connectPublisherToFeature( context, featurePath ) which creates an action publisher for the given feature and adds that publisher function as a property to the given context's actions object, under the corresponding path, creating the object as needed.

If the given action feature is not configured for a widget instance, the function is still created, but as a no-op. The new method is intended to simplify creating widgets where actions should be triggered directly from a widget template.

Examples

First, let's assume that we are writing a widget that has feature configuration for a doStuff.action (so, it acts as an outlet for that action):

AngularJS example:

Widget Controller:

patterns.actions.connectPublisherToFeature($scope, 'doStuff');

Widget Template

<button ng-click="actions.doStuff()">click me</button>
<button ng-click="actions.doStuff({ myPayload: [] })">click me, with payload</button>
React example:

Widget Component

patterns.actions.connectPublisherToFeature(context, 'doStuff');
// ...
axReactRender(
   <div>
      <button onClick={context.actions.doStuff}></button>
      <button onClick={ () => context.actions.doStuff({ myPayload: [] }) } >
         click me, with payload
      </button>
   </div>
)