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

Define a variable's attribute using another attribute of itself #153

Open
gliechtenstein opened this issue Jul 24, 2017 · 1 comment
Open

Comments

@gliechtenstein
Copy link
Contributor

It would be nice to be able to use _variables not just from functions but from attributes. For example in the following code I have to create 2 separate variables (template and c), and refer to template from c's _add() function:

var template = function(index) {
  return {
    class: "page hidden",
    $text: index,
    style: {
      zIndex: index,
      color: "white",
      background: '#'+Math.floor(Math.random()*16777215).toString(16)
    },
    $init: function() {
      this.class = "page";
    },
    onclick: function(e) {
      this._add();
    }
  }
};
var c = {
  $cell: true,
  _add: function() {
    this.$components.push(template(++this._index))
  },
  _index: 0,
  $components: [template(0)]
}

It would be nice if this could be somehow expressed as :

var c = {
  $cell: true,
  _add: function() {
    this.$components.push(this._template(++this._index))
  },
  template:  function(index) {
    return {
      class: "page hidden",
      $text: index,
      style: {
        zIndex: index,
        color: "white",
        background: '#'+Math.floor(Math.random()*16777215).toString(16)
      },
      $init: function() {
        this.class = "page";
      },
      onclick: function(e) {
        this._add();
      }
    }
  },
  _index: 0,
  $components: [this._template(0)]
}

Currently this is not possible because that's how JavaScript works--you can't refer to an object's attribute from its own definition. (In above case the this in this._template will be bound to the window object.

Even though this is not natively possible with JS maybe there's a clever way around it.

@Caffeinix
Copy link

What if $components accepted either an array or a function returning an array? Then we could have:

var c = {
  $cell: true,
  _add: function() {
    this.$components.push(this._template(++this._index))
  },
  template:  function(index) {
    return {
      class: "page hidden",
      $text: index,
      style: {
        zIndex: index,
        color: "white",
        background: '#'+Math.floor(Math.random()*16777215).toString(16)
      },
      $init: function() {
        this.class = "page";
      },
      onclick: function(e) {
        this._add();
      }
    }
  },
  _index: 0,
  $components: function() { return [this._template(0)]; }
}

The function would be invoked whenever $components would otherwise be read (so not at $init time or $update time).

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

2 participants