项目作者: cevou

项目描述 :
SAP OpenUI5 model to build applications with redux
高级语言: JavaScript
项目地址: git://github.com/cevou/openui5-redux-model.git
创建时间: 2017-01-31T23:04:07Z
项目社区:https://github.com/cevou/openui5-redux-model

开源协议:Apache License 2.0

下载


OpenUI5 Redux Model

Travis
Coveralls

This repository provides a OpenUI5 library to integrate OpenUI5 with Redux. This makes it
possible to write applications with predictable state with SAP OpenUI5.

Redux is a predictable state container for JavaScript apps.
It helps you write applications that behave consistently, run in different
environments (client, server, and native), and are easy to test.

Install

To use this library download it via bower (like all other OpenUI5 packaged libraries).

  1. bower install openui5-redux-model

Then list the library in the UI5 bootstrap tag.

  1. <script id="sap-ui-bootstrap"
  2. src="resources/sap-ui-core.js"
  3. data-sap-ui-libs="redux">
  4. </script>

Make sure that the library files are server in the right directory. This is dependent on from where you load the
UI5 libraries.

You also need to integrate Redux somewhere in your application. It is not included with this library.
You can do this by loading it from a CDN via a <script> tag or by including the redux.js file directly somewhere
in your application.

By default Redux is available in a global variable. However, you can also include it via the UI5 module system.
To do that you need to register it with the UI5 core.

  1. jQuery.sap.registerModuleShims({
  2. 'app/thirdparty/redux.min': {
  3. exports: 'Redux'
  4. }
  5. });

Usage

To use the redux model you first have to instantiate a redux store. You can find more information about what options
are available in the redux documentation. Then you can instantiate the Model and set it for the UI5 Core, your
component, ….

  1. var oStore = Redux.createStore(fnReducer);
  2. var oModel = new ReduxModel(oStore);
  3. sap.ui.getCore().setModel(oModel);

Now you can bind to the data in the store in your views by simply providing the path in the state (similar to a JSON
model).

  1. <Button text="{/test}" ></Button>

To update the data in your view you just dispatch actions via your store and process them using your reducer(s).

Advanced Usage

It is not a best practice to access data in your store directly because the structure of how your data is stored can
easily change as you develop your application. The better approach is to use selectors with a defined API. This
selectors are simple functions which get the current state passed as an argument and return data based on that.

You can use selectors with the redux model. Just pass an object with selector functions when you create your model.

  1. <Button text="{/selector/selectorFunction}" ></Button>

All selector binding must start with /selector. The second part of the binding in property name of the object in which the
selector is defined.

  1. var oStore = Redux.createStore(fnReducer);
  2. var oModel = new ReduxModel(oStore, {
  3. selectorFunction: function(state, context) {
  4. }
  5. });
  6. sap.ui.getCore().setModel(oModel);

The first on in the current state and the second one the (optional) context.