简体   繁体   中英

EmberJs Function from Handlebars

I'm learning Ember js by myself and I have this sample code, which works fine:

<script>
    window.EmberApp = Ember.Application.create();
    var Marcapagina = Ember.Object.extend({
         convertir_en_link: function() {
            return "<a href='" + this.get("url") + "'>"
            + this.get("nombre")
            + "</a>";
    },
     nombre: "Robert App",
     url: "http://www.google.cl"
    });
</script>

And in the html body I get the link printed, this way:

<script type="text/javascript">
$(document).ready(function(){
 $("#here").append(marcapagina.convertir_en_link());
});
</script>
<div id="here"></div>

My question is, is there a way to call the function from handlebars, like:

<body>
<script type="text/x-handlebars" data-template-name="index">
Here is where i want to print the result of the function, but
{{marcapagina.convertir_en_link()}}
Doesn't works!!!
</script>

I want to get rid of this piece of code:

<script type="text/javascript">
$(document).ready(function(){
 $("#here").append(marcapagina.convertir_en_link());
});
</script>
<div id="here"></div>
</body>
</html>

Your approach is using Ember.Objects in a way that is not intended. But the end result that you're looking for is a very basic and included part of Ember.

Here's a working jsbin that shows the "Ember Way"

If you're just starting out with ember, I would highly recommend walking through the Todo MVC tutorial right here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM