0

I'm creating with backbone a view for a meta tag that I need it to be in the head for obvious reasons.

The problem is that the output result I'm having is the meta inside another meta container. So I get this:

<head>
    <meta>
         <meta name="description" content="The description of the meta">
    </meta>
</head>

I'm creating it like this:

    var Useroptions = Backbone.View.extend({
        initialize : function(d){
            this.data = d.obj.data;
            this.template = Handlebars.compile(d.obj.template);
            d.parent.model.bind('change:category', this.openCategory);
            this.render();
        },
        render: function(){
            this.$el.html(this.template(this.data))
        }
    })

    var App = Backbone.View.extend({
        el : 'body',
        model : new AppModel(),
        initialize : function(){
            var _this = this;
            this.useroptions = new Useroptions({
                obj : _app_.useroptions,
                el : $('<meta />').appendTo('head'),
                parent : this
            });
    });

Any idea on how to get this without the container so my output would be:

<head>
    <meta name="description" content="The description of the meta">
</head>
propcode
  • 299
  • 1
  • 5
  • 15
  • Something like this http://stackoverflow.com/questions/11594961/backbone-not-this-el-wrapping/11598543#11598543 ? – nikoshr Jun 05 '15 at 12:31
  • @nikoshr thanks. But do I have to remove the `el : $('').appendTo('head')` in the `App`? – propcode Jun 05 '15 at 12:39
  • No, the render method I linked would replace it instead of appending to it. – nikoshr Jun 05 '15 at 12:42
  • I replaced my `render: function` with your method. And replaced `html= /**however you build your html **/;` with `html= "";` It outputs in the `head` just the tag: `` without any content :/ – propcode Jun 05 '15 at 12:48
  • Well, if you set your HTML to an empty meta, you will get ``. Try `html = ''`; . And here's a demo http://jsfiddle.net/nikoshr/axnxe61z/ – nikoshr Jun 05 '15 at 12:51
  • I see, the thing is that i'm getting the meta description of a custom field from a `view > template`. I can't hardcode it there because I want the user to be able to change it from the content editor. – propcode Jun 05 '15 at 12:54
  • 1
    Then `html = this.template(this.data)` – nikoshr Jun 05 '15 at 12:56
  • Wow man, it works! thank you very much. Add it as an answer so I can award it to you :) – propcode Jun 05 '15 at 12:58
  • I'll mark your question as a duplicate if you don't mind, the answer is the same if you omit a few wrinkles we worked out in the comments. – nikoshr Jun 05 '15 at 13:00

0 Answers0