Skip to content

Use JSPatch development of functional modules

LastDays edited this page Aug 11, 2016 · 1 revision

JSPatch can be Used to develop functional modules,The part of the business entirely JS Development,Dynamic sent to the client and executed,Let APP also has a native experience and dynamic characteristics,And Use oc thinking,Use all OC librarys and JS librarys for development。

Use JSPatch development function module has some new interfaces are recommended:

##autoConvertOCType()

When JSPatch for NSDictionary / NSArray / NSString / NSData these types passed from OC will not automatically be converted to JS JS corresponding Object / Array / String / Date type,For these types, JS there are two objects,One is the OC target objects on which another native JS [Detailed description](https://github.com/bang590/JSPatch/wiki/JSPatch-常见问题#字符串 / 数组 / 字典 操作问题)

In the development of this function block will cause greater distress,At the beginning of implementation of the recommendations

autoConvertOCType(1);

So that all NSDictionary / NSArray / NSString / NSDate will automatically return to JS JS into a corresponding type, JS on only one type, and can no longer call these types of OC Method:

//OC
@implementation JPTestObject
+ (NSMutableDictionary *)info {
  return @{@"k": @"v"};
}
+ (NSArray *)users {
  return @[@"alex", @"bang", @"cat"];
}
@end
autoConvertOCType(1);

var info = JPTestObject.info();
var users = JPTestObject.users();

console.log(info['k']); 
console.log(users[0]);

info.objectForKey('k');   //crash
users.objectAtIndex(1);   //crash




//When the provisional must call these objects OC method, you can turn off and then turn on the automatic conversion:

autoConvertOCType(0);

var ocInfo = JPTestObject.info();
console.log(ocInfo.objectForKey('k'))    //OK

autoConvertOCType(1);

##defineJSClass()

Many classes are created when the development of functional,Some are inherited from the OC needs classes,For example VC layer needs to inherit UIView and UIViewController。Some do not need to inherit the class OC,For example class M layer for data manipulation。Creating these classes need not actually occur in JS relationship with OC,Recommended defineJSClass () define these classes, there will be greatly improved performance.

For example:

defineJSClass('JPBaseDataSource', {
  init: function() {
    this.baseData = [1,2,3];
  }
});
var _dataSourceShareInstance;

defineJSClass('JPDataSource : JPBaseDataSource', {
  init: function(){
    this.super().init();
    this.data = this.baseData.concat([4,5,6]);
    return this;
  },

  readDataAtIndex: function(i) {
    return this.data[i]
  },

}, {
  shareInstance: function(){
    if (!_dataSourceShareInstance) {
      _dataSourceShareInstance = JPDataSource.alloc().init();
    }
    return _dataSourceShareInstance;
  },
})
var dataSource = JPDataSource.shareInstance();
dataSource.readDataAtIndex(1)

You can see defineClass () for use with defineClass () almost the same,Method definition / object generation / instance methods and class methods defined / inherited wording / super wording is the same,Only two differences:

  1. Use this keyword instead of self
  2. property without getter / setter, direct access.

##include() / resourcePath()

You can use include () interfaces introduce other js file path is relative execution of the main program directory, for example:

 - js
  - main.js
  - JPDataSource.js
  - controllers
    - JPViewController.js
  - views
    - JPView.js
  - img
    - demo.png

In main.js main program (incoming[JPEngine evaluateScript WithPath: @ ""]execution,Directory is the root directory,Fill this include the relative directory path:

//main.js
include('JPDataSource.js');
include('controllers/JPViewController.js');
//JPViewController.js
include('views/JPView.js');

Actual path through resourcePath () Get the resource file, the path rule and include ()

//JPView.js
var path = resourcePath('img/demo.png');
var image = require('UIImage').imageWithContentsOfFile(path);

##Other

DefineClass () method to create if it is determined not to call in OC (not protocol / target action method),Users will not be called,private method,Recommend beginning with an underscore _ named,This can reduce unnecessary OC method to register.

##Demo

Other wording may referDribbble Demo

Clone this wiki locally