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

Issue regarding Backbone and twig.js #83

Closed
alexis-gruet-deel opened this issue Jun 20, 2014 · 5 comments
Closed

Issue regarding Backbone and twig.js #83

alexis-gruet-deel opened this issue Jun 20, 2014 · 5 comments

Comments

@alexis-gruet-deel
Copy link

Suppose that a backbone model MyModel has "public" methods such as :

getFoo() { return 'foo' );

Now suppose we hydrate n MyModel in a Backbone Collection MyCollection.
In a backbone View it will looks like this :

render: function() {
   this.$el.html ( Twig.render( tpl.name, { collection : this.MyCollection }) ) ;
}

Unfortunately,

{% for model in collection %}
  {{ model.getFoo() }}  
  {{ model.get( 'attr' ) }}
{% endfor %}

doesn't work and return void.
the only thing working right now to access attributes of a Backbone Model is to Jsonize the collection itself :

//this.MyCollection.toJSON() 
this.$el.html ( Twig.render( tpl.name, { collection : this.MyCollection.toJSON()  }) ) ;

and access attrs thru :

{{ model.attr }}

It's restrictive... As an example the _.template() from underscore allow to access the whole methods

Is a normal behavior of twig.js ?

Best,

@alexis-gruet-deel alexis-gruet-deel changed the title Backbone and twig.js Question regarding Backbone and twig.js Jun 20, 2014
@alexis-gruet-deel alexis-gruet-deel changed the title Question regarding Backbone and twig.js Issue regarding Backbone and twig.js Jun 20, 2014
@henrycatalinismith
Copy link
Contributor

I've just added a new integration test to the suite in an attempt to reproduce your problem. Here's the test I wrote:

--TEST--
Method call test for schmittjoh/twig.js#83
--TEMPLATE--

{{ object.getFoo() }}
{{ object.get("attr") }}

--DATA--
{
  object: {
    getFoo: function() {
      return "foo";
    },
    get: function(name) {
      return "foo" + name;
    }
  }
}
--EXPECT--
foo
fooattr

The test passes though, so I'm pretty confident that method calls are working okay. I was wondering, have you tried using collection.models instead?

{% for model in collection.models %}
  {{ model.getFoo() }}  
  {{ model.get( 'attr' ) }}
{% endfor %}

@alexis-gruet-deel
Copy link
Author

Hi @Urael and thx for your answer,

I did some tests :

Adding the "models" attribute to the collection like :

{% for model in collection.models %}
    {{ model.getFoo() }}
{% endfor %}

Solved the issue for something 'static' meaning outside of the this.get()

getFoo: function() { return 'foo'; } 

Otherwise, if :

getFoo() 

proxy a call to native

Backbone.Model.get( 'myAttr' )

like :

getFoo: function() { 
// ...Work hard here
    return this.get( 'myAttr' );
}

Void is returned.

Calling the native Backbone.Model.get: function( attr ) from a template thru :

{% for model in collection.models %}
   {{ model.get( 'attr' ) }}
{% endfor %}

Still remains void.

Maybe, is more a issue related to Backbone.Model... however while looking inside it's only a getter accessing to a hash. Nothing huge :)

Backbone.Model.get: function(attr) {
      return this.attributes[attr];
},

Best,

@henrycatalinismith
Copy link
Contributor

This looks like it might be a symptom of issue #67. We only merged the fix for that bug ten days ago, so I wouldn't be surprised if you were using a version of Twig.js still affected by it. Would you mind updating your Twig.js to the very latest version and checking if your bug is still present?

@alexis-gruet-deel
Copy link
Author

It rocks!

Thanks to the merge f2a5e5d

And of course thanks for your advices @Urael :)

Best,

@henrycatalinismith
Copy link
Contributor

Glad to hear it :)

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