将表单发布数据设置为骨干模型以呼叫休息服务

时间:2014-02-10 13:41:18

标签: javascript jquery json rest backbone.js

我是骨干的新人。我已经创建了一个表单

<form name="searchForm" method="POST" id="CandidateSearch"
        `        
        enctype="application/json" accept-charset="utf-8">
        <br> <br>
        <div class="container" style="background-color: #eee;">

            <div class="rows" id="row1" style="background-color: #eee;">
                <!----Heading section--->
                <h2 class="adcase">Advance Candidate Search (Job)</h2>
            </div>
            <br>
            <div class="rows" id="row2" style="background-color: #eee;">
                <!----Skill section--->
                <div class="rows">
                    <h3 class="skill2" style="margin-top: 20px;">Skill:</h3>
                    <br>
                    <div class="side-by-side clearfix">
                        <div>
                            <select data-placeholder="Choose a option..." name="skills"
                                class="chosen-select" multiple style="width: 350px;"
                                tabindex="4">
                                <option value="sonal" class="a3a">asd</option>
                                <option value="United States" class="a3a">It</option>
                                <option value="United Kingdom">auto</option>
                                <option value="Afghanistan">bpo</option>
                                <option value="Aland Islands">Aland Islands</option>
                                <option value="Albania">Albania</option>
                                <option value="Algeria">Algeria</option>
                                <option value="American Samoa">American Samoa</option>
                                <option value="Andorra">Andorra</option>
                                <option value="Angola">Angola</option>
                                <option value="Anguilla">Anguilla</option>
                                <option value="Antarctica">Antarctica</option>
                                <option value="Antigua and Barbuda">Antigua and Barbuda</option>
                                <option value="Argentina">Argentina</option>
                            </select>
                        </div>
                    </div>
                    <br> <br>
                </div>
            </div>
            <br>

            <div class="rows" id="row3" style="background-color: #eee;">
                <!---Location section--->
                <h3 class="skill2" style="margin-top: 20px;">Location:</h3>
                <br>
                <div class="col-md-2">
                    <select id="mark" name="country" class="btgredse">
                        <option value="">--</option>
                        <option value="india">india</option>
                        <option value="Audi">Audi</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select id="series" name="state" class="btgredse">
                        <option value="">--</option>
                        <option value="raj" class="india">raj</option>
                        <option value="mah" class="india">mah</option>
                        <option value="kar" class="india">kar</option>
                        <option value="A3" class="Audi">A3</option>
                        <option value="A4" class="Audi">A4</option>
                        <option value="A5" class="Audi">A5</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select id="model" name="city" class="btgredse">
                        <option value="">--</option>
                        <option value="pune" class="mah">pune</option>
                        <option value="jaipur" class="raj">jaipur</option>
                        <option value="hubli" class="kar"cla">hubli</option>
                        <option value="Sportback" class="a3 a5">Sportback</option>
                    </select>
                </div>
            </div>
            <br> <br>

            <div class="rows" id="row4" style="background-color: #eee;">
                <!----------Domain and role section--->
                <h3 class="skill2" style="margin-top: 20px;">Domain & Role:</h3>
                <div class="col-md-6" id="row4-c1" style="background-color: #eee;">
                    <h4 class="skill2" style="margin-top: 20px;">Functional Area :
                    </h4>
                    <br>
                    <div class="side-by-side clearfix">
                        <div>
                            <select data-placeholder="Choose a option..."
                                name="functionalArea" class="chosen-select" multiple
                                style="width: 350px;" tabindex="4">
                                <option value=""></option>
                                <option value="United States">It</option>
                                <option value="United Kingdom">auto</option>
                                <option value="Afghanistan">bpo</option>
                                <option value="Aland Islands">Aland Islands</option>
                                <option value="Albania">Albania</option>
                                <option value="Algeria">Algeria</option>
                                <option value="American Samoa">American Samoa</option>
                                <option value="Andorra">Andorra</option>
                                <option value="Angola">Angola</option>
                                <option value="Anguilla">Anguilla</option>
                                <option value="Antarctica">Antarctica</option>
                                <option value="Antigua and Barbuda">Antigua and Barbuda</option>
                                <option value="Argentina">Argentina</option>
                            </select>
                        </div>
                    </div>
                    <br> <br>
                </div>
                <div class="col-md-6" id="row4-c2" style="background-color: #eee;">
                    <h4 class="skill2" style="margin-top: 20px;">Functional Role:
                    </h4>
                    <br>
                    <div class="side-by-side clearfix">
                        <div>
                            <select data-placeholder="Choose a option..."
                                name="functionalRole" class="chosen-select" multiple
                                style="width: 350px;" tabindex="4">
                                <option value=""></option>
                                <option value="United States">testing</option>
                                <option value="United Kingdom">designing</option>
                                <option value="Afghanistan">develpment</option>
                                <option value="Aland Islands">Aland Islands</option>
                                <option value="Albania">Albania</option>
                                <option value="Algeria">Algeria</option>
                                <option value="American Samoa">American Samoa</option>
                                <option value="Andorra">Andorra</option>
                                <option value="Angola">Angola</option>
                                <option value="Anguilla">Anguilla</option>
                                <option value="Antarctica">Antarctica</option>
                                <option value="Antigua and Barbuda">Antigua and Barbuda</option>
                                <option value="Argentina">Argentina</option>
                            </select>
                        </div>
                    </div>
                    <br> <br>
                </div>
            </div>

            <div class="rows" id="row5" style="background-color: #eee;">
                <!-------education section--->
                <h3 class="skill2" style="margin-top: 20px;">Education:</h3>
                <br>
                <div class="col-md-2">
                    <select id="Highestd" class="btgredse" name="highestDegree">
                        <option name="name" value="1">Highest Degree</option>
                        <option name="name" value="2">Be</option>
                        <option name="name" value="3">MCA</option>
                        <option name="name" value="4">ME</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select id="Specialization" class="btgredse" name="specialization">
                        <option name="name" value="1">Specialization</option>
                        <option name="name" value="2">jodhpur</option>
                        <option name="name" value="3">kota</option>
                        <option name="name" value="4">churu</option>
                        <option name="name" value="5">siker</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <select id="To1" class="btgredse" name="passingYear"><option
                            name="name" value="1">Passing year</option></select>
                </div>
            </div>
            <br> <br>

            <div class="rows" id="row9" style="background-color: #eee;">
                <!-------Institute section--->
                <h3 class="skill2" style="margin-top: 20px;">Experience:</h3>
                <br>
                <div class="col-md-1">
                    <select id="Min" class="btgredsesmage" name="totalExperienceMin"><option
                            name="name" value="1">Min</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <select id="Max" class="btgredsesmage" name="totalExperienceMax"><option
                            name="name" value="1">Max</option>
                    </select>
                </div>
            </div>
            <br> <br>


            <div class="rows" id="row10" style="background-color: #eee;">
                <h3 class="" style="margin-top: 20px;">Keyword:</h3>
                <br>
                <div class="col-md-2">
                    <input type="text" name="keyWord">
                </div>
            </div>
            <br> <br> <br> <br>
            <div class="rows" id="row11" style="background-color: #eee;">
                <div class="col-md-12">
                    <center>
                        <button type="submit" class="btn btn-default btn-lg" id="submit1">Submit</button>
                    </center>
                </div>
            </div>


        </div>



    </form>

我正在尝试获取表单数据以便调用休息服务。我通过谷歌但没有找到。

<script type="text/javascript">
            var Search = Backbone.Model.extend({

            });
            var search =new Search();
            $('#CandidateSearch').submit(function() {

                 var $this = $(this)
                    , viewArr = $this.serializeArray()
                    , view = {};

                  for (var i in viewArr) {
                    view[viewArr[i].name] = viewArr[i].value;
                  }                 

                });
        </script>

我希望从

这样的形式开始使用JSON
{
"skills":["c","java"],
"country":["India"],
"state":["Maharashtra","Gujrat"],
"city":["Mumbai","Pune"],
"highestDegree":["MCA","BE"],

"functionalArea":["IT"],
"functionalRole":["Tester"]
}

但它给了我输出作为对象。我缺少的地方?

1 个答案:

答案 0 :(得分:0)

你的提交功能对我来说是正确的,除了它不会产生任何东西。我会建议像:

var Search = Backbone.Model.extend({
  url: "/some/url"
});

$('#CandidateSearch').submit(function() {

  var $this = $(this),
      viewArr = $this.serializeArray()
      search = new Search();

  for (var i in viewArr) {
    search.set(i, viewArr[i]);
  }

  search.save({
    success: function() { ... }
  });

  return false;
});

需要注意的是:

  1. Backbone模型提供了许多有用的功能,例如setsave http://backbonejs.org/#Model
  2. 不要忘记return false末尾的submit!否则,浏览器继续正常的POST请求。
  3. 如果可以,我还建议为您的表单创建一个Backbone.View,然后您可以将提交事件绑定为“骨干方式”。