最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - backbone passing parameters to a model url - Stack Overflow

matteradmin4PV0评论

In my controller I'm passing in a parameter called 'url' to a view. This is passed successfully into the initialize function. Next when I create my model I would like to pass the same parameter into the model to use as the rooturl.

Below is what I have tried but I got the following error message:

A "url" property or function must be specified 

This seems like the parameter is not being passed to the model (undefined) or I'm not calling it correctly in the model. What I'm I doing wrong here?

Note: console.log(options.url) in the initialize function view confirms the parameter is passed successfully from the controller to the view, the issues is from the view to the model where it is undefined.

Controller:

var myview = new NewView({
           url:"api/"
       })**

View:

  var myview = Backbone.Marionette.ItemView.extend({

        initialize: function (options) {
            this.url = options.url;
            this.model.fetch();
        },


    model: new myModel({
            url: this.url
        }),

myModel:

var myModel = Backbone.Model.extend({
  urlRoot: function(){ return this.get('url') }

});

In my controller I'm passing in a parameter called 'url' to a view. This is passed successfully into the initialize function. Next when I create my model I would like to pass the same parameter into the model to use as the rooturl.

Below is what I have tried but I got the following error message:

A "url" property or function must be specified 

This seems like the parameter is not being passed to the model (undefined) or I'm not calling it correctly in the model. What I'm I doing wrong here?

Note: console.log(options.url) in the initialize function view confirms the parameter is passed successfully from the controller to the view, the issues is from the view to the model where it is undefined.

Controller:

var myview = new NewView({
           url:"api/"
       })**

View:

  var myview = Backbone.Marionette.ItemView.extend({

        initialize: function (options) {
            this.url = options.url;
            this.model.fetch();
        },


    model: new myModel({
            url: this.url
        }),

myModel:

var myModel = Backbone.Model.extend({
  urlRoot: function(){ return this.get('url') }

});
Share Improve this question asked Nov 15, 2013 at 15:31 PrometheusPrometheus 33.7k58 gold badges174 silver badges311 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You are trying to fetch your model with an empty url.

When you call:

initialize: function (options) {
  this.url = options.url;
  this.model.fetch();
}

fetch here, you don't have the url for the model. So, you could do:

initialize: function (options) {
  this.url = options.url;
  this.model.set({url: this.url});
  this.model.fetch();
}

When writing

model: new myModel({
        url: this.url
    }),

the view is not yet initialized

You can instantiate the model in the view's initialize method

initialize: function (options) {
        this.url = options.url;
        this.model = new myModel({
            url: this.url
        })
        this.model.fetch();
    },
Post a comment

comment list (0)

  1. No comments so far