2013-03-04 44 views
5

好吧,所以這開始讓我瘋狂。我現在已經搜索並搜索了幾個小時,並且每個解決方案都不適合我。所以,是的,這個問題可能是多餘的,但我不能爲我的生活得到解決方案的工作。jQuery Mobile和Knockout.js模板,樣式不適用

我有一堆由通過knockout.js數據綁定的jquery模板生成的複選框。然而,它變得沒有風格。 Afaik,這是關於jquery mobile的事情,在knockout渲染模板之前的樣式,所以它最終沒有風格。 我嘗試過無數方法無濟於事,所以我希望這裏有人能看到我做錯了什麼。

(我使用jQuery Mobile的1.2.0,jQuery的1.8.2和2.2.1淘汰賽)

這是腳本:

<script type="text/javascript">  


jQuery.support.cors = true; 

var dataFromServer = "";  
// create ViewModel with Geography, name, email, frequency and jobtype 
var ViewModel = { 
    email: ko.observable(""), 
    geographyList: ["Hovedstaden","Sjælland","Fyn + øer","Nordjylland","Midtjylland","Sønderjylland" ], 
    selectedGeographies: ko.observableArray(dataFromServer.split(",")), 
    frequencySelection: ko.observable("frequency"), 
    jobTypes: ["Kontor (administration, sekretær og reception)","Jura","HR, Ledelse, strategi og udvikling","Marketing, kommunikation og PR","Handel og service (butik, service, værtinde og piccoline)","IT","Grafik og design","Lager, chauffør, bud mv.","Økonomi, regnskab og finans","Kundeservice, telefoninterview, salg og telemarketing","Sprog","Øvrige jobtyper"], 
    selectedJobTypes: ko.observableArray(dataFromServer.split(",")), 
    workTimes: ["Fulltid","Deltid"], 
    selectedWorkTimes: ko.observableArray(dataFromServer.split(",")) 
}; 

// function for returning checkbox selection as comma separated list 
ViewModel.selectedJobTypesDelimited = ko.dependentObservable(function() { 
    return this.selectedJobTypes().join(","); 
}, ViewModel); 

var API_URL = "/webapi/api/Subscriptions/"; 

// function used for parsing json message before sent 
function omitKeys(obj, keys) { 
    var dup = {}; 
    var key; 
    for (key in obj) { 
    if (obj.hasOwnProperty(key)) { 
     if (keys.indexOf(key) === -1) { 
     dup[key] = obj[key]; 
     } 
    } 
    } 
    return dup; 
} 

//Function called for inserting new subscription record 
function subscribe() { 
    if($("#jobmailForm").valid()=== true){ 
    //window.alert("add subscriptiooncalled"); 
    var mySubscription = ko.toJS(ViewModel); 
    //var json = JSON.stringify(mySubscription); 
    var jsonSmall = JSON.stringify(omitKeys(mySubscription, ['geographyList','jobTypes','selectedJobTypesDelimited','workTimes'])); 
    //window.alert(jsonSmall); 
    $.ajax({ 
     url: API_URL, 
     cache: false, 
     type: 'POST', 
     contentType: 'application/json', 
     data: jsonSmall, 
     success: function (data) { 
      window.alert("success"); 

     }, 
     error: function (error) { 
      window.alert("ERROR STATUS: " + error.status + " STATUS TEXT: " + error.statusText); 

     } 
    }); 
    } 
} 

function initializeViewModel() { 
    // Get the post from the API  
    var self = this; //Declare observable which will be bind with UI 
    // Activates knockout.js 
    ko.applyBindings(ViewModel); 
} 

// Handle the DOM Ready (Finished Rendering the DOM) 
$("#jobmail").live("pageinit", function() { 
    initializeViewModel(); 
    $('#jobmailDiv').trigger('updatelayout'); 
}); 


</script> 
    <script id="geographyTmpl" type="text/html"> 
    <input type="checkbox" data-role="none" data-bind="attr: { value: $data }, attr: { id: $data }, checked: $root.selectedGeographies" /> 
    <label data-bind="attr: { for: $data }"><span data-bind="text: $data"></span></label> 
    </script> 
    <script id="jobTypeTmpl" type="text/html"> 
    <label><input type="checkbox" data-role="none" data-bind="attr: { value: $data }, checked: $root.selectedJobTypes" /><span data-bind="text: $data"></span></label> 
    </script> 

注意, 「職位郵件」 是周圍「頁面「div元素,這裏沒有顯示。這是標記:

<div data-role="content"> 
<umbraco:Item field="bodyText" runat="server"></umbraco:Item> 
<form id="jobmailForm" runat="server" data-ajax="false"> 
    <div id="jobmailDiv"> 
    <p> 
    <label for="email">Email</label> 
    <input type="text" name="email" id="email" class="required email" data-bind="'value': email" /> 
    </p> 

    <fieldset data-role="controlgroup" data-mini="true" data-bind="template: { name: 'geographyTmpl', foreach: geographyList, templateOptions: { selections: selectedGeographies } }"> 
    <input type="checkbox" id="lol" /> 
    <label for="lol">fkfkufk</label> 
    </fieldset> 
    <fieldset data-role="controlgroup" data-mini="true"> 
    <p data-bind="template: { name: 'jobTypeTmpl', foreach: jobTypes, templateOptions: { selections: selectedJobTypes } }"></p> 
    </fieldset> 

    <fieldset data-role="controlgroup" data-mini="true"> 
    <input type="radio" id="frequency5" name="frequency" value="5" data-bind="checked: frequencySelection" /><label for="frequency5">Højst 5 gange om ugen</label> 
    <input type="radio" id="frequency3" name="frequency" value="3" data-bind="checked: frequencySelection" /><label for="frequency3">Højst 3 gange om ugen</label> 
    <input type="radio" id="frequency1" name="frequency" value="1" data-bind="checked: frequencySelection" /><label for="frequency1">Højst 1 gang om ugen</label> 
    </fieldset> 

    <p> 
    <input type="button" value="Tilmeld" class="nice small radius action button" onClick="subscribe();"> 
    </p> 

    <a href="{locallink:1733}" data-role="button" data-icon="back" data-inline="true" data-direction="reverse">Tilbage</a> 
</div> 
</form> 

調用改型(不工作要麼)的可替代的方法:

$(document).on('pagebeforeshow', '#jobmail', function(){  
// Get the post from the API  
    var self = this; //Declare observable which will be bind with UI 
    // Activates knockout.js 
    ko.applyBindings(ViewModel); 
}); 
// Handle the DOM Ready (Finished Rendering the DOM) 
$("#jobmail").live("pageinit", function() { 
    $('#jobmail').trigger('pagecreate'); 
}); 

回答

3

每個動態生成jQuery Mobile內容必須手動增強。

它可以在幾個方面來完成,但是最常見的一種可以通過jQuery Mobile功能.trigger(來完成。

實施例:

  • 僅增強頁面內容

    $('#page-id').trigger('create'); 
    
  • 增強全頁(頭+含量+頁腳):

    $('#page-id').trigger('pagecreate'); 
    

如果你想找到更多關於這個話題看看我的其他ARTICLE,要更透明這是我的個人博客。或找到它HERE

+0

謝謝您的回覆,這篇小文章對未來的參考很有幫助。我嘗試了你的建議,並且我的原始文章中的代碼略有差異,但都無效。 我試着把在pagebeforeshow和pageinit上重新打開文件的敲除綁定的代碼,聽起來合理,但不工作。 你有什麼時候在我的代碼中應該觸發的建議嗎? – 2013-03-05 08:29:08

+0

它應該放置在所有複選框生成後。如果你在一個循環內進行,那麼觸發('create')必須在循環之後。並且不要爲每個循環迭代都這樣做,否則會減慢頁面生成。因此,只有在所有動態內容生成後才執行一次。 – Gajotres 2013-03-05 08:35:26

+0

但這就是我一直在努力做的,沒有運氣。我完全不知所措。 – 2013-03-05 12:00:53

5

使用自定義綁定(Knockout)觸發jQuery Mobile到增強由Knockout生成的動態創建內容。

下面是一個簡單的自定義綁定:

ko.bindingHandlers.jqmEnhance = { 
    update: function (element, valueAccessor) { 
     // Get jQuery Mobile to enhance elements within this element 
     $(element).trigger("create"); 
    } 
}; 

使用自定義您的HTML這樣的結合,其中myValue是改變,引發了動態內容插入到DOM您的視圖模型的一部分:

<div data-bind="jqmEnhance: myValue"> 
     <span data-bind="text: someProperty"></span> 
     <a href="#some-id" data-role="button">My Button</a> 
     <input type="radio" id="my-id" name="my-name" value="1" data-bind="checked: someOtherProperty" /><label for="my-id">My Label</label> 
    </div> 

以我自己的情況下,是myValue表達式的一部分在if結合,這將觸發內容被添加到DOM。

<!-- ko if: myValue --> 
    <span data-bind="jqmEnhance: myValue"> 
     <!-- My content with data-bind attributes --> 
    </span> 
    <!-- /ko --> 
+0

何時運行jqmEnhance更新?你如何知道在內容創建之後而不是之前會發生這種情況? – 2014-02-06 16:42:43

+0

根據[文檔](http://knockoutjs.com/documentation/custom-bindings.html): *每當關聯的observable發生變化時,KO會調用您的更新回調函數* – GiddyUpHorsey 2014-02-08 06:27:05

+0

好的,所以MyValue應該是最後一個在所有其他更改觸發更新後更改對象。 – 2014-02-09 18:33:32