-1

I am learning backbone.js and I have seen some examples like this one.Here the user has not written much html codes inside the editor.html.Only 4 lines of code.But for colour change,size change etc he has done inside editor.js

Please help me understand what all codes we need to keep inside .html file

<div id="page" style="width:2000px;height:2000px;">
    <button id="new-rectangle">New Rectangle</button>
    <button id="new-circle">New Circle</button>
</div>
rocking
  • 4,729
  • 9
  • 30
  • 45
  • @downvoter I know you are talented person,But As I said I am a learner,If you can not help me and can not encourage then please do not discourage me also – rocking Feb 12 '14 at 04:10
  • I believe the reason for the downvote (it wasn't me) is that you don't have a specific problem along the lines of "this is broken, here is what I've tried to do to fix it. What am I doing wrong". StackOverflow is geared to answer these kind of questions, not genral "best practices" questions. As it stands your question is very broad. – Jon P Feb 12 '14 at 04:20
  • @JonP Thanks for sharing information,I have seen many questions like where can I download backbone js,how to start etc.So I thought we can also ask this type of questions.And also for beginners in the future may get benefitted – rocking Feb 12 '14 at 04:23

2 Answers2

1

You should aim to put all your html in .html file(s). As an app grows, it will help you to keep them separate. The example you link to is a 'simplified' version - this is not how you would structure things in an actual app. You would load html from templates in the render function. A (though this is also simplified as I am relying on script tags) pattern would be:

HTML file:

[...SOME HTML...]
<script type="text/html" id="template-contact">
  <div class='contact'>
    <h1>Here's my template code</h1>
    <strong>name</strong>
    <span>email</span>
  </div>
</script>

Then in your Backbone view render function:

 render: function() {

   template: _template($('#template-contract').html(),

    this.$el.html(this.template()); 
    return this;
 }

Then somewhere else in your Backbone code you create a new instance of the view and render it.

var example = new view_name();

example.render(); //This loads the html template 

If you need to dynamically load the html from a server, you can use underscore (or whichever template engine you are using) tags in your template '<%>' and use models. This is best explained in Addy Osmani's book Developing Backbone.js Applications which, incredibly, is free. Here's the link to the relevant section

cs_stackX
  • 1,407
  • 2
  • 19
  • 27
  • Thanks for the answer,+1.In this [question](http://stackoverflow.com/questions/21750110/how-to-collect-all-the-images-from-a-folder) though I accepted the answer but I didnt find it useful.Can you post your answer and also can tell me if the fiddle is as per backbone MVC? – rocking Feb 14 '14 at 04:24
  • Can I expect a reply from you? – rocking Feb 14 '14 at 05:32
  • Glad you found it useful - I've got to take care of something else now, so can't look at the other question. Might be able to check it out tomorrow. – cs_stackX Feb 14 '14 at 05:34
  • ohh yes,But please have a look and answer if possible.Thanks again.I will upvote your answer also – rocking Feb 14 '14 at 05:35
-1

Whatever you wants to display on the browser you can keep it in .html file and logic to update the dom on run time should be in .js file.

Mahesh Thumar
  • 4,257
  • 5
  • 22
  • 30
  • Thanks for the answer,I hope you are not the downvoter.If I want to show different images then where will I keep.For example there are 3 buttons.If click 1st button then show the 1st image and click on the 2nd button then show the 2nd image – rocking Feb 12 '14 at 04:19
  • Put image tags in html file with 1st visible and other two hidden(display:none), now logic goes in js for clicking the button change visibility accordingly. – Mahesh Thumar Feb 12 '14 at 04:28
  • Thanks again.If the images are dynamic,then where will I keep the images. – rocking Feb 12 '14 at 04:38
  • If images are dynamic then whole logic will go in js. No need to be thanks, if answer is useful then mark it as answer. I can't help you in down-vote of your post. – Mahesh Thumar Feb 12 '14 at 04:40