Backbonejs:从集合中获取点击模型

时间:2014-02-17 07:07:44

标签: backbone.js underscore.js backbone-collections

define(['jquery', 'underscore', 'backbone', 'text!views/manageUsers.tpl', 'common'], function($, _, Backbone, tmpl_manageUsersView, ajax) {

/*
 *    Module list
 *
 *    tmpl_page1View      templates/tmpl_page1View.html
 */

var manageUsersView = Backbone.View.extend({
    // Setting the view's template property using the Underscore template method        
    template: _.template(tmpl_manageUsersView),
    // View constructor
    initialize: function() {
        self = this;
    },
    // View Event Handlers
    events: {

    },
    // Renders the view's template to the UI
    render: function() {
        this.$el.html(this.template({data: this.templateData}));
        user=Backbone.Model.extend({
            defaults:{
                name:"",
                password:"",
                isAdmin:false
            },

        });
        users=Backbone.Collection.extend({
            model:user,
            url:"auth"
        });

        usersCollection=new users();
        usersCollection.fetch({
            error:function(response,xhr){
                console.log(response);
            },
            success:function(response){
                        count=1;
                _.each(data,function(d){
                    if(count%2==0)
                        $("#users>tbody").append("<tr class='odd'><td>"+count+"</td><td>"+d.username+"</td><td><a href='#' class='edit-iocn' id='edit_"+d.username+"' ></a><a class='ancrul delete-icon' id='delete_"+d.username+"'></a></td>");
                    else
                        $("#users>tbody").append("<tr class='even'><td>"+count+"</td><td>"+d.username+"</td><td><a href='#' class='edit-iocn' id='edit_"+d.username+"'></a><a class='ancrul delete-icon' id='delete_"+d.username+"'></a></td>");
                    count++;
                });
                /*--*/

                var oTable = $('#users').dataTable({
                    "sDom": '<"bottom"<i>rtp<"clear">',
                    //"sDom":'<"top"ip>rt<"bottom"ip<"clear">',

                    "sPaginationType": "full_numbers",
                    "bLengthChange": true,
                    "bPaginate": true,
                    "bInfo": true,
                    //"bAutoWidth": true,
                    "iDisplayLength":5,
                    "oLanguage": {
                        "sInfo": "",
                        "sInfoEmpty": ""
                    },
                    });

            }
        });

});
return manageUsersView;
});

以上是我从网址获取数据的代码。

关注manageUsers.tpl文件。

<div class="content">
<p> </p>
<h3></h3>
<div class="admin-login-area ui-corner-all">
    <p class="validateTips">All form fields are required.</p>
    <form id="addUserForm">
        <fieldset>
        <label for="name" class="login-label">User Name</label>
        <input type="text" name="u-name" id="u-name" class="text ui-widget-content ui-corner-all">
        <label for="name" class="login-label">Password</label>
        <input type="password" name="p-name" id="p-name" class="text ui-widget-content ui-corner-all">
        <label for="email" class="login-label">Retype Password</label>
        <input type="password" class="text ui-widget-content ui-corner-all" id="c-name" name="c-name">
        <input type="checkbox" id="isAdmin" />
        <label>Is Admin</label>
        <label class="login-label"></label>
        <br/>
        <input type="button" name="submit" id="submit" value="Submit" class="submit-btn">
        <input type="button" name="submit" id="reset" value="Reset" class="submit-btn">
        <input type="button" name="submit" id="cancel" value="Cancel" class="submit-btn">
        <label class="login-label"></label>         
      </fieldset>
      </form>

<!-- end admin login --></div>
<div class="table" >

  <table width="100%" cellspacing="0" cellpadding="0" border="0"  id="users">
  <thead>

            <th>Sr No</th>
            <th>users</th>
            <th>Actions</th>

            </thead>
            <tfoot style="display: table-header-group;">
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>

                </tr>
            </tfoot>
            <tbody>

            </tbody>
        </table>

  </div>

并且在成功获取数据时我已经用collectios的数据填充了表格,并且点击表格中的项目我想要检索整个模型。我在同一个tpl文件中有表格和表格

如何使用backbonejs和下划线js?

1 个答案:

答案 0 :(得分:1)

首先,将此事件添加到您的视图中:

events : {
    'click .edit-iocn' : 'edit', // in your code you typed `iocn` instead of `icon`
    'click .delete-icon' : 'delete'
}

在没有id='"+d.username+"'edit_

的情况下,将ID更改为delete_

最后编辑和删除方法:

edit: function(event) {
    var username = event.currentTarget.id;

    var user = usersCollection.where({name: username})[0];
    ...
}

delete: function(event) {
    var username = event.currentTarget.id;

    var user = usersCollection.where({name: username})[0];
    ...
}