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

New Method API #141

Closed
kbrsh opened this issue Sep 10, 2017 · 7 comments
Closed

New Method API #141

kbrsh opened this issue Sep 10, 2017 · 7 comments
Labels

Comments

@kbrsh
Copy link
Owner

kbrsh commented Sep 10, 2017

Moon v1 will be getting a new method API that uses actual method calls instead of instance.callMethod(). Here is what it will look like:

  1. Methods will be treated as constants. Moon will no longer deoptimize m-on because it cannot infer if you will be changing the methods. In v1, Moon will enforce that all methods are constant and cannot change.
  2. Methods will no longer be a part of instance.data, but instead will be stored in instance.methods. This means that you can have data and methods with the same name (although this is a bad practice).
  3. Methods will be callable with:
    instance.methods.foo("Bar", 10);
    This also means that m-on will attach event listeners for methods differently, and will only deoptimize if a dynamic parameter is passed (m-on:click="foo(dynamicProperty)")

@rogierverbrugge @LeviSchuck @kokujin @trusktr @karlseguin I'd love to know your thoughts on this (you guys are pretty active on here).

@kbrsh kbrsh added the feature label Sep 10, 2017
@LeviSchuck
Copy link

I favor the dot calls more than string accessors.
I am cool with all methods being constant to the component, not changing between instances or current properties.
No comment on the m- stuff, as I've ended up writing render code directly with greater predictability.

@kbrsh kbrsh mentioned this issue Sep 10, 2017
@kbrsh
Copy link
Owner Author

kbrsh commented Sep 10, 2017

Cool @LeviSchuck. I prefer them as well, and they will not be present in data anymore. I have a feeling that this change will be welcomed (I certainly like it), and am working on the changes right now.

By the way, you can use functional components for a little speed boost (if you're writing custom render functions). The way functional components work is that they can have data, props, and inserts.

This will only work for v1.0.0-pre-alpha.

Moon.extend("Functional", {
  functional: true,
  data: () => ({
    foo: "Local Data"
  }),
  props: ["bar"],
  render: function(m, context) {
    // Data AND props are available in `context.data`
    // Insert is available in `context.insert`
    return m("h1", {attrs: {}}, {dynamic: 1}, [
      m("#text", {dynamic: 1}, `Local Data: ${context.data.foo} | Props: ${context.data.bar}`)
    ]);
  }
});

@kbrsh kbrsh closed this as completed in a83fd4e Sep 10, 2017
@trusktr
Copy link

trusktr commented Sep 18, 2017

Why is it that instance.foo("Bar", 10); isn't possible?

@kbrsh
Copy link
Owner Author

kbrsh commented Sep 18, 2017

@trusktr It is possible, but that would mean you can't name methods as any of Moon's internal properties.

@sudomabider
Copy link

I dig this.

@kbrsh
Copy link
Owner Author

kbrsh commented Sep 18, 2017

@trusktr @sudomabider If you really want them on the instance (and will be careful to name them correctly), then you will be able to use this in v1:

const MoonMethods = {
  init(Moon) {
    const MoonInit = Moon.prototype.init;
    Moon.prototype.init = function() {
      const methods = this.methods;
      for(let name in methods) {
        this[name] = methods[name];
      }
      MoonInit.call(this);
    }
  }
};

Moon.use(MoonMethods);

const app = new Moon({
  methods: {
    foo() {
      alert("Foo Called!");
    }
  }
});

app.foo();

@trusktr
Copy link

trusktr commented Sep 19, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants