Using Backbone LocalStorage and still making calls to the server

為{幸葍}努か 提交于 2019-12-12 03:55:06

问题


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(),


来源:https://stackoverflow.com/questions/19469079/using-backbone-localstorage-and-still-making-calls-to-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!