简体   繁体   English

bone.js无法获取单个模型

[英]backbone.js cannot fetch a single model

I'm having a problem fetching a model that is outside of a collection. 我在获取集合外的模型时遇到问题。 I have seen many solutions and for some reason none seem to be working (probably doing something stupid). 我已经看到了许多解决方案,由于某种原因,它们似乎都不起作用(可能做的很愚蠢)。 I have a success function in my actual fetch which never fires. 我的实际提取中有一个成功函数,永远不会触发。 It should be hitting /devices/{deviceId} 应该是/ devices / {deviceId}

Here is my code 这是我的代码

define([
  'underscore',
  'backbone',


], function(_, Backbone) {
  // Creating backbone model
  var DeviceModel = Backbone.Model.extend({
    // set defaults
    defaults: {
    },

    idAttribute: "deviceId",
    urlRoot: '/devices'
  })
  return DeviceModel;
});

and I am calling it from 我从这里打电话

        model = new DeviceModel({deviceId: this.deviceId })

        model.fetch()

Thank you! 谢谢!

You could try to pass callback functions to debug what is going on with: 您可以尝试传递回调函数来调试发生的情况:

  model.fetch()

From the documentation of BackboneJS Model-Fetch BackboneJS模型获取文档

Accepts success and error callbacks in the options hash, which are both passed (model, response, options) as arguments. 接受选项哈希中的成功和错误回调,它们均作为参数传递(模型,响应,选项)。

So, you can include: 因此,您可以包括:

model.fetch({success: function(m, resp) { console.log(resp);}, 
               error: function(m, resp) { console.log(resp);} 
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM