Git Product home page Git Product logo

Comments (3)

jtrussell avatar jtrussell commented on June 14, 2024

Unless I'm mistaken that's just how scoping works in JS:

function Foo() {
  this.whatIsThis = function() {
    console.log(this);
  };
}

var f = new Foo();

f.whatIsThis();
// -> Foo {}

var hmmm = f.whatIsThis;
hmmm();
// -> Window {...}

When you pass a reference to select to the directive it is no longer implicitly bound to a Controller instance. If you'd like it to retain that context you'll need to bind it yourself or do something of the sort.

Edit:

I can see how this might be confusing when compared to something like ngClick - in that case they're evaluating an expression and we're just calling a function as given. I think we'd be probably be alright with moving to a similar approach of evaluating an expression.

from angular-ivh-treeview.

digitalkaoz avatar digitalkaoz commented on June 14, 2024

@jtrussell sure thats scoping in JS.

but thats not the desired behavior.

i could think of a additional option like ivh-treeview-callback-scope="category" which binds the callbacks to this scope.

or as you said use the ng-click approach.

we cant bind ourself to a specific scope, because angular doesnt allows it:
Error: [$parse:isecff] Referencing call, apply or bind in Angular expressions is disallowed! Expression: category.select.bind(category)

if i try it like this:

ivh-treeview-click-handler="category.select.bind(category)"

from angular-ivh-treeview.

jtrussell avatar jtrussell commented on June 14, 2024

Angular may not let you bind directly in an expression but there's no reason you can't bind your function elsewhere. Or make a local reference to this if you need it to be preserved regardless of how your function is called:

function Foo() {
  var self = this;

  this.whatIsThis = function() {
    console.log(this);
  };

  this.whatWasThis = function() {
    console.log(self);
  };

  this.whatWasThis2 = function() {
    console.log(this);
  }.bind(this);
}

var f = new Foo();
  , a = f.whatIsThis
  , b = f.whatWasThis
  , c = f.whatWasThis2

a(); // --> Window {...}
b(); // --> Foo {}
c(); // --> Foo {}

The reason we went with callbacks rather than $evaled expressions (a la ngClick) was so we could pass references to the node being clicked and the owning tree to the callback. I added an item to our v1 roadmap (#27) to investigate making a switch to having these be expression based. I don't expect we'll introduce any breaking changes until the v1 release though so until then you'll be stuck working with callback references and all that implies. Feel free to comment here (#55) as well.

from angular-ivh-treeview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.