简体   繁体   English

如何在基于Backbone.js的模板中向元素添加数据

[英]How to add data to an element in Backbone.js based template

I have a project in my hand, and it used backbone.js (which I have never used). 我手上有一个项目,它使用了ribs.js(我从未使用过)。 Though I have studied the code, and read many things on the net for Backbone. 尽管我已经研究了代码,并且在Backbone上阅读了很多东西。 Now, I have a question, how should I edit the content of an element, after the AJAX request is successful? 现在,我有一个问题,在AJAX请求成功后,应该如何编辑元素的内容? This ajax request is triggered by a button-click, the button is bound to a click-event. 此ajax请求是通过单击按钮触发的,该按钮绑定到单击事件。 All nested in Backbone. 全部嵌套在骨干网中。

There is a button in the template, which is bound to an event, when the button is clicked, then an ajax request is sent to the server. 模板中有一个与事件绑定的按钮,单击该按钮时,会将ajax请求发送到服务器。 On the response, I want to add a message to the template, here are the codes: 关于响应,我想向模板添加一条消息,以下是代码:

Tempplate: 模板:

<div id='main-view'>
<button id='delete-users'>Delete All Users</button>
</div>
<div class='message-container'></div>

Backbone: 骨干:

Event: 事件:

'click #users-list' : 'usersList',
'click #delete-users' : 'deleteUsers',

View: 视图:

usersFetch.then(function(){
    var Users = new App.Views.Users({ collection : usersCollection})
    creatDataTable();
});

Deleting User 删除用户

(event for button) (按钮事件)

deleteUsers: function(){


               $.ajax({
                   url : "<?php echo url("users/delete/all"); ?>",
                   type : "GET",
                   success : function(data){
                        if(data.result == true)
                        {
                             // Here I want to add a message to the
                             // .message-container
                             console.log("Successfully Done.");
                        }
                        else if(data.result == false)
                        {
                            console.log("An error occurred.");
                        }
                   },
                   error :  function(data){
                        console.log("An error Occurred.");
                   }
               });
            }

Now I want to add a message to the .message-container instead of console login. 现在,我想向.message-container添加一条消息,而不是控制台登录。 How should I do that then? 那我该怎么办呢?

In your success callback simply find the message container scoped to that Backbone View . 在您的success回调中,只需找到范围为该Backbone View的消息容器。 The way you would do this is by using the Backbone jQuery selector this.$ , as follows: 您可以通过使用Backbone jQuery选择器this.$来做到这一点,如下所示:

success : function(data){
   if(data.result == true)
   {
       that.$('div.message-container').text("Successfully Done.");
   }
   else if(data.result == false)
   {
       that.$('div.message-container').text("An error Occurred.");
   }
},

Were I defined var that = this; 我是否定义了var that = this; before the $.ajax() call. $.ajax()调用之前。

Your other option would be to rerender your view and pass the days to your template. 您的另一个选择是重新渲染视图并将日期传递给模板。 That's more costly and more complex. 这更昂贵,更复杂。 It also depends on how you're rendering your templates. 它还取决于您如何呈现模板。


the jQuery delegate for element lookup is defined in the Backbone source as: 元素查找的jQuery委托在Backbone源中定义为:

$: function(selector) {
  return this.$el.find(selector);
}

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

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