Handle bar 사용 사례

5786 단어
    <script type="text/javascript" src="../../Scripts/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="../../Scripts/handlebars.min.js"></script>
    <script type="text/javascript" src="../../Scripts/linq.min.js"></script>
<body>
        <!--*******************Layouts**************************-->
        <div id="layout_list">
            
        </div>
        <div id="layout_operation">
           
        </div>
        

        <!-----------------------------------------------------
            *************************List Template******************
            **************************************************-->       
        <script id="t_list" type="text/x-handlebars-template">
  <table id="tblList">
    <thead>
      <th>Username</th>
      <th>Real Name</th>
      <th>Email</th>
    </thead>
    <tbody>
      {{#users}}
        <tr>
          <td>
            <a id="lnkViewDetails" href="#" pk="{{Id}}">{{username}}</a></td>
          <td>{{firstName}} {{lastName}}</td>
          <td>{{email}}</td>
        </tr>
      {{/users}}
    </tbody>
  </table>

</script>
        
        <!--
            ------------------Operate Template 
            -->

        <script id="t_operation" type="text/x-handlebar-template">
        <div>UserName : <input type="text" value="{{username}}" id="txtOpUserName" /></div>
            <div>First Name : <input type="text" value="{{firstName}}" id="txtOpFirstName" /></div>
            <div>Last Name: <input type="text" value="{{lastName}}" id="txtOpLastName" /></div>
            <div>Email : <input type="text" value="{{email}}" id="txtOpEmail" /></div>

            <input type="text" style="display:none" id="hdnOpId" value="{{Id}}" />
           
            <input type="button" id="btnSave" value="Save/Add" />
            <input type="button" id="btnDelete" value="Delete" />
        </script>
        

        <script type="text/javascript">
            
            //DataContext which should be retrieved from Controller
            var data = {
                users: [
                    {Id:1, username: "alan", firstName: "Alan", lastName: "Johnson", email: "[email protected]" },
                    {Id:2, username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]" },
                    {Id:3, username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]" }
                ]
            };
            
            //*********
            //Actions
            //*********
            
            //render list
            //datacontext+temlate ->compile->html
            //replace html->layout html
            
            //on load , apply list template , show data list
            applyT("layout_list", "t_list", data);
            
            //Details
            //on click each item , apply template > add event 
            //ONCE AGAIN .for each click ,need to add event 
            $("#tblList").find("tr td a").click(function() {
                var id = $(this).attr("pk");
                
                var user = Enumerable.From(data.users).First(function(u) {
                    return u.Id == id;
                });
                applyT("layout_operation", "t_operation", user);

                //Save
                $("#btnSave").click(function () {
                    
                    ////post current changes to controller
                    PostAction("SaveOrAdd");
                });
                //Delete
            });
           
            
            //******Common****************
            
            //$.Ajax
          
            
            $(document).ajaxError(function (event, xhr) {
                alert(xhr.status + ":" + xhr.statusText);
            });
            
            function PostAction(name) {
                var student = {
                    Id: $("#hdnOpId").val(), FirstName: $("#txtOpFirstName").val(),
                    LastName: $("#txtOpLastName").val(), UserName: $("#txtOpUserName").val(), Email: $("#txtOpEmail").val()
                };
                var url = "/Test/" + name;
                debugger;
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: student,
                    success: function (obj) {
                        alert(JSON.stringify(obj.Entity.Data));
                    },
                    dataType: "json"
                    
                });

            }
                                                                               

            //Apply Template to #layout
            function applyT(layoutId,templateId,data) {
                $("#"+layoutId).html(Handlebars.compile($("#"+templateId).html())(data));
            }

        </script>

    </body>

좋은 웹페이지 즐겨찾기