-->

Using Backbone LocalStorage and still making calls

2019-09-01 20:59发布

问题:

How do I make a call to the server using Backbone localStorage I saw this question, but I am not sure where I apply it?

Backbone.js able to do rest and localstorage?

Here is my code for the view:

define([
  'jquery',
  'underscore',
  'backbone',
  'models/song',
  //'collections/songs',
  //'views/song',
  'text!templates/search.html'

], function($, _, Backbone, SearchM, SearchT){ //Song, Songs, SongV, 

  var Search = Backbone.View.extend({
    model: SearchM,
    el: $("#Sirius"),
    events: {
      'submit #searchMusic': 'search'
    },
    search: function (search) {
      console.log(SearchM);
      this.model.save({
        channel: this.$el.find('#channel'),
        week: this.$el.find('#week'),
        year: this.$el.find('#year'),
        filter: this.$el.find('#filter')
      });
      console.log('saved');
    },
    render: function () { 
      this.$el.html( SearchT );

    }
  });
    return Search;
});

Here is the model:

define([
  'underscore',
  'backbone'
], function(_, Backbone) {

  var Search = Backbone.Model.extend({
    url: '/music'
  });
  return Search;
});

this.model.save does not work giving me this error:

Uncaught TypeError: Object function (){return r.apply(this,arguments)} has no method 'save' 

I am trying to make an ajax-post to my server at that url.

回答1:

I've got an error because you haven't instantiate your model. Instead

 model: SearchM,

you should have

 model: new SearchM(),