Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从获取Django auth后端API的主干模型中获取属性_Javascript_Django_Backbone.js - Fatal编程技术网

Javascript 如何从获取Django auth后端API的主干模型中获取属性

Javascript 如何从获取Django auth后端API的主干模型中获取属性,javascript,django,backbone.js,Javascript,Django,Backbone.js,我正在一个项目中使用django auth后端、django REST框架API和主干 var User = Backbone.Model.extend({ urlRoot: host + 'api/users/' }); // django auth response var isAuthenticated = {{ request.user.is_authenticated|yesno:"true,false" }}; if (isAuthenticated){ var userI

我正在一个项目中使用django auth后端、django REST框架API和主干

var User = Backbone.Model.extend({
  urlRoot: host + 'api/users/'
});

// django auth response
var isAuthenticated = {{ request.user.is_authenticated|yesno:"true,false" }};
if (isAuthenticated){
  var userID = {{ request.user.id }}; // django user id
  console.log(userID); // checking value
  var currentUser = new User({id: userID});
  currentUser.fetch();
  var username = currentUser.get('username');
  console.log(currentUser); // checking value
控制台日志的结果(当前用户)

如何读取
属性:Object

类似于,
fetch
是异步的

您可以在控制台中看到数据,因为您探索对象所花的时间足以完成API的往返

Chrome开发工具控制台保留一个对正在记录的对象的引用,然后当用户与它交互时(点击一次折叠对象),会有一点
,您可以阅读:

刚才评估了下面的值

主干网提供了您可以传递的不同选项,如
success
回调

currentUser.fetch({
    success: function() {
        console.log("username:", currentUser.get('username'));
    }
});
知道了这一点,在主干视图中,您可以用来知道属性何时可以使用

var View = Backbone.View.extend({
    initialize: function() {
        this.listenTo(this.model, 'sync', this.render);
        this.model.fetch();
    },

    // this is called when the model's sync event is fired, so
    // when the model's attributes are ready.
    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});
“同步”
(模型或收集、响应、选项)
-当模型或 集合已成功与服务器同步


嘿,约丹,我假设你在使用Django Rest框架?你能发布你的API是如何工作的吗?views.py可能会有所帮助,以及当前响应返回的内容。
{“url”:http://127.0.0.1:8000/api/users/1/“,”用户名“:”yorsant“,”电子邮件“:”y*******@****m“,”名字“:”姓氏“:”,“is_staff”:true}
api结果。我编辑了这个问题,模型正在抓取,我不知道如何获取值。我使用的是ModelViewSet和HyperlinkedModelSerializer。你有使用ajax的经验吗
fetch
是异步的,它需要往返到服务器。谢谢@EmileBergeron,我现在更明白了。
var View = Backbone.View.extend({
    initialize: function() {
        this.listenTo(this.model, 'sync', this.render);
        this.model.fetch();
    },

    // this is called when the model's sync event is fired, so
    // when the model's attributes are ready.
    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});