Commit 79187d6e authored by alexandre's avatar alexandre
Browse files

api >< backbone with nested models

parent 2568bc36
;window.W = window.W || {};
;(function(undefined) {
'use strict';
Backbone.Relational.store.addModelScope(W);
W.ScoreModel = Backbone.RelationalModel.extend({
urlRoot: '/api/scores/',
relations: [{
type: Backbone.HasOne,
key: 'children',
relatedModel: 'AxisModel',
collectionType: 'AxisCollection',
// reverseRelation: {
// key: 'parent',
// includeInJSON: 'id'
// // 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
// }
}]
});
W.AxisModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'children',
relatedModel: 'AxisModel',
collectionType: 'AxisCollection',
// reverseRelation: {
// key: 'parent',
// includeInJSON: 'id'
// // 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
// }
}]
});
W.AxisCollection = Backbone.Collection.extend({
model: W.AxisModel
});
W.ScoreView = Backbone.Marionette.View.extend({
el: 'body',
template: _.template('Contents'),
initialize: function() {
this.listenTo(this.model, 'change', this.render);
},
});
var score = new W.ScoreModel({id: 1});
var myView = new W.ScoreView({model: score});
score.fetch();
})();
{% load static %}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.radio/2.0.0/backbone.radio.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.marionette/3.2.0/backbone.marionette.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone-relational/0.10.0/backbone-relational.min.js"></script>
<script src="{% static 'playground/js/models.js' %}"></script>
<script src="{% static 'playground/js/app.js' %}"></script>
</body>
</html>
from django.conf.urls import url
from playground.views import SandboxView
from playground.views import ScoreDetailView, SandboxView
urlpatterns = [
url(r'^sandbox/$', SandboxView.as_view(), name='sandbox'),
url(r'^score/(?P<pk>\d+)/$', ScoreDetailView.as_view(), name='score-detail'),
]
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
from rest_framework import viewsets
from .models import Score
......@@ -12,6 +13,12 @@ class ScoreViewSet(viewsets.ModelViewSet):
serializer_class = ScoreSerializer
class ScoreDetailView(DetailView):
"""
"""
model = Score
class SandboxView(TemplateView):
"""
"""
......
......@@ -13,6 +13,8 @@
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.radio/2.0.0/backbone.radio.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/3.2.0/backbone.marionette.min.js"></script>
<script charset="utf-8">
var model = Backbone.Model.extend({
defaults: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment