CptCrash80
CptCrash80

Reputation: 5

Getting a "Object doesn't support property or method ;Save;" when trying to save a Backbone.js model

I'm new to Backbone and am currently trying to get the Save() method working on my individual models. I have everything else working at the moment, but when I call Save() on an individual item it tells me that the method doesn't exist. Any ideas? Thanks in advance.

Code:

            var Root = this;

            //MODELS
            var Option = Backbone.Model.extend({});

            var BooleanOption = Option.extend({
                initialize: function () {
                    this.constructor.__super__.initialize.apply(this, arguments);
                    if (this.get("ValueAsString") === "Y") { this.set("IsChecked", true); };
                },
                IsChecked: false
            });

            var SelectOption = Option.extend({
                initialize: function () {
                    this.constructor.__super__.initialize.apply(this, arguments);
                    this.set("Options", this.get("ValidationValue").split(","));
                },
                Options: []
            });

            var TextOption = Option.extend({
                initialize: function () {
                    this.constructor.__super__.initialize.apply(this, arguments);
                    this.set("MaxLength", Number(this.get("ValidationValue").replace("x", "")));
                },
                MaxLength: null
            });

            //main collection model
            var OptionsCollection = Backbone.Collection.extend({
                model: function (attr, options) {
                    switch (attr.ValidationType) {
                        case "B":
                            return new BooleanOption(attr, options);
                            break;
                        case "O":
                            return new SelectOption(attr, options);
                            break;
                        case "C":
                            return new TextOption(attr, options);
                            break;
                        default:
                            return new Option(attr, options);
                            break;
                    }
                },
                urlBase: "http://localhost:40217/Shared/Options.svc/",
                url: function () {
                    return this.urlBase + Root.getParam("ModuleID") + "?true";
                }
            });
            //END MODELS

            //VIEWS
            var OptionView = Backbone.View.extend({
                render: function (eventName) {

                }
            })


            var BooleanOptionView = OptionView.extend({
                initialize: function () {
                    this.listenTo(this.model, "change", this.render);
                },

                render: function (eventName) {
                    $("#content").append(this.el);
                    $(this.el).html(_.template($('#boolean-option-template').html(), this.model));
                    return this;
                },

                events: {
                    "change .chkBox": "test"
                },

                test: function () {
                    alert("valueChanged");
                }
            });

            var SelectOptionView = OptionView.extend({
                initialize: function () {
                    this.listenTo(this.model, "change", this.render);
                },

                render: function (eventName) {
                    $("#content").append(this.el);
                    $(this.el).html(_.template($('#select-option-template').html(), this.model));
                    return this;
                },

                events: {
                    "change .selectOption": "test"
                },

                test: function () {
                    alert("valueChanged");
                }
            });

            var TextOptionView = OptionView.extend({
                initialize: function () {
                    this.listenTo(this.model, "change", this.render);
                },

                render: function (eventName) {
                    $("#content").append(this.el);
                    $(this.el).html(_.template($('#text-option-template').html(), this.model));
                    return this;
                },

                events: {
                    "change .textOption": "test"
                },

                test: function () {
                    alert("valueChanged");
                }
            });

            var MainView = Backbone.View.extend({
                render: function (eventName) {
                    $("#content").append(this.el);
                    $(this.el).html(_.template($('#main-template').html(), this.model));
                    _.each(this.model.models, function (opt) {
                        if (opt.get("ValidationType") === "B") {
                            new BooleanOptionView({ model: opt }).render();
                        }
                        else if (opt.get("ValidationType") === "C") {
                            new TextOptionView({ model: opt }).render();
                        }
                        else if (opt.get("ValidationType") === "O") {
                            new SelectOptionView({ model: opt }).render();
                        }
                    }, this);
                    return this;
                },

                events: {
                    "click .saveBtn": "saveOptions"
                },

                saveOptions: function () {
                    _.each(this.model.models, function (mod) {
                        mod.Save(mod.attributes);
                    })
                }
            });

            //END VIEWS

            $(document).ready(function () {
                var oc = new OptionsCollection();
                oc.fetch({
                    success: function () {
                        $("#content").append(new MainView({model:oc}).render().el);
                    }
                });
            });

            function getParam(sname) {
                var params = location.search.substr(location.search.indexOf("?") + 1);
                var sval = "";
                params = params.split("&");
                // split param and value into individual pieces
                for (var i = 0; i < params.length; i++) {
                    temp = params[i].split("=");
                    if ([temp[0]] == sname) { sval = temp[1]; }
                }
                return sval;
            }

Upvotes: 0

Views: 232

Answers (1)

Ryan O&#39;Neill
Ryan O&#39;Neill

Reputation: 1790

Javascript is case sensitive.

mod.save(mod.attributes); // Instead of mod.Save

Upvotes: 1

Related Questions