Skip to content

defineClass in JSPatch, target actions pattern and selector #9

Closed
@lancy

Description

@lancy
Contributor

Hey

I noticed that defineClass (JPEngine.m:144) only override those methods that already exsit in the OC class; otherwise the new methods of user define stay in JS, which mean you can't call your new methods in OC.

So you can't use the target actions pattern and selector, and I guess is really important:

var button = UIButton.alloc().init()
// It doesn't work, and you can't create selector like that for now.
button.addTarget_action_forControlEvents(self, @selector("buttonTapped:", 0)

In the demo, it doesn't work when handleBtn doesn't exist:

// JPViewController
@implementation JPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 50)];
    [btn setTitle:@"Push JPTableViewController" forState:UIControlStateNormal];

    // try to call @selector(handleBtn:), which define in demo.js
    [btn addTarget:self action:@selector(handleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:btn];
}

/** comment this and it will crash
- (void)handleBtn:(id)sender
{
}
*/
@end
// demo.js
defineClass('JPViewController', {
  handleBtn: function(sender) {
    var tableViewCtrl = JPTableViewController.alloc().init()
    self.navigationController().pushViewController_animated(tableViewCtrl, YES)
  },
})
...

I guess we can covert all the user define JS functions into OC methods(override if exist, add new one if not) to solve this problem, and define a new way to convert string(or something else) to SEL from JS to OC.

Any suggestion?

Activity

bang590

bang590 commented on May 26, 2015

@bang590
Owner

@lancy the problem is if we want to add method to an OC class dynamically, we should know the type of all params and return value, but the JS function didn't mention these types. I'm figuring out a way to pass types from JS to OC, would be updated soon.

leafduo

leafduo commented on May 27, 2015

@leafduo
Contributor

@bang590 Objective-J would be a probably solution.

bang590

bang590 commented on May 27, 2015

@bang590
Owner

Solved in 653074f. We can add new method to OC class now.
Thank you for the excellent advice.
demo:

defineClass('JPViewController: UIViewController', {
  viewDidLoad: function() {
    self.super.viewDidLoad();
    var width = require('UIScreen').mainScreen().bounds().width
    var btn = require('UIButton').alloc().initWithFrame({x:0, y:100, width:width, height:50})
    btn.setTitle_forState('Push JPTableViewController', 0)
    btn.addTarget_action_forControlEvents(self, "handleBtn:", 1 << 6)
    btn.setBackgroundColor(require('UIColor').grayColor())
    self.view().addSubview(btn)
  },
  handleBtn: function(sender) {
    var tableViewCtrl = JPTableViewController.alloc().init() 
    self.navigationController().pushViewController_animated(tableViewCtrl, 1)
  }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @leafduo@bang590@lancy

        Issue actions

          defineClass in JSPatch, target actions pattern and selector · Issue #9 · bang590/JSPatch