bobamo /plugins/generator/views/modelcollections.js

Language Javascript Lines 56
MD5 Hash d93e02dfd4b05817ebf6fdabf0438bfa
Repository git://github.com/jspears/bobamo.git View Raw File
| Open JSFiddle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
define([
    'underscore',
    'Backbone'
], function(_, Backbone) {
    "use strict";

    //we define these together because they often link together and if they are in seperate callbacks bad things happen.

    var schema = {{html JSON.stringify(model.schemaFor(model.fieldsets || model.edit_fields))}};
    var defaults = {{html JSON.stringify(model.defaults)}};

    var Model = Backbone.Model.extend({
        urlRoot:'${api}/${model.modelName}',
        schema:schema,
        defaults:defaults,
        initialize: function() {},
        parse:function(resp) {

            console.log('/${api}/${model.modelName}model#parse', resp);

            return resp.payload ? resp.payload : resp;
        },
        get:function(key){
            if (key && key.indexOf('.') > -1){
                var split = key.split('.');
                var val = this.attributes;
                while (split.length && val)
                    val = val[split.shift()];

                return val;
            }
            return Backbone.Model.prototype.get.call(this, key);
        }

    });
    var Collection = Backbone.Collection.extend({
        model: Model,
        url:'${api}/${model.modelName}',
        initialize: function() {
             this.total = 0;
        },
        parse:function(resp) {
            console.log('Collection#parse ${model.modelName}', resp.payload);
            this.total = resp.total;
            return resp.payload ? resp.payload : resp;
        }
    });


    return {
        Model:Model,
        Collection:Collection
    };

});
Back to Top