Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defineClass in JSPatch, target actions pattern and selector #9

Closed
lancy opened this issue May 26, 2015 · 3 comments
Closed

defineClass in JSPatch, target actions pattern and selector #9

lancy opened this issue May 26, 2015 · 3 comments

Comments

@lancy
Copy link
Contributor

lancy commented May 26, 2015

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?

@bang590
Copy link
Owner

bang590 commented May 26, 2015

@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
Copy link
Contributor

leafduo commented May 27, 2015

@bang590 Objective-J would be a probably solution.

@bang590
Copy link
Owner

bang590 commented May 27, 2015

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants