model fetch

<!DOCTYPE html>
<html>
<head>
<title>php backbone.js example 1</title>
<scriptsrc=”node_modules/jquery/dist/jquery.js”></script>
<scriptsrc=”node_modules/underscore/underscore.js”></script>
<scriptsrc=”node_modules/backbone/backbone.js”></script>
</head>
<body>
<divid=”msg”></div>
<!– Templates –>
<scripttype=”text/template”id=”tpl-hello-backbone”>
<%= message %>
</script>
<!– JavaScript –>
<script>
varMessageModel = Backbone.Model.extend({
urlRoot :’example_1.php’,
defaults: {
message:”Text Message”
}
});
var MessageView = Backbone.View.extend({
template:_.template($(‘#tpl-hello-backbone’).html()),
render:function (eventName) {
$(this.el).html(this.template(this.model.toJSON()));
returnthis;
}
});
var MessageRouter = Backbone.Router.extend({
routes:{
“”:”displayMessage”
},
displayMessage:function() {
varmessageModel = newMessageModel();
varmessageView = newMessageView({model:messageModel});
messageModel.fetch({
success:function () {
$(‘#msg’).html(messageView.render().el);
}
});
}
});
var messageRouter = new MessageRouter();
Backbone.history.start();
</script>
<!–
fisierul php
echo ‘{“message”:”Hello Backbonejs”}';
–>
</body>
</html>