2013-01-23 67 views
2

我正在使用requirejs爲我的頁面加載JavaScript。我有一個webApi路由,可以從文件中動態讀取,並使用Newtonsoft JObject返回JSON。然後在客戶端,將結果分配給本地JavaScript變量。webapi返回的動態JavaScript

定義([ 'jQuery的'],函數($){

function dynoFile() { 
    $.get('/api/resources/dynofile', function (results) { 
     myNamespace.dynoFile = results; 
    }); 
} 

return new dynoFile(); }); 

這個例子並工作,但它會導致與預期myNamespace.dynoFile存在其他JavaScript文件的問題。由於這個文件加載得很好,其他文件不會等待$ .get完成。

是否有可能讓web api方法只返回JavaScript並讓瀏覽器將其識別爲JavaScript而不僅僅是文本?我試圖在中設置響應標題web api和多種方式來返回生成的腳本。

這可能嗎?

UPDATE:

要進入更詳細一點,我用我的Web API來處理我的資源文件,並返回JSON的客戶端,因爲它是一個單頁的應用程序。我希望從我的Web API中返回JavaScript,我可以使用RequireJS來加載。我現在把它作爲JSON工作,並認爲我會分享我的。

這裏是我的WebAPI方法讀取一個資源文件,並將它作爲JSON:

public JObject Get() 
{ 
    var data = new JObject(); 

    var type = typeof(Translations); 
    var properties = type.GetProperties(); 

    foreach (var property in properties) 
    { 
     if (property.Name != "ResourceManager" && property.Name != "Culture") 
     { 
      data.Add(property.Name, property.GetValue(type, null).ToString()); 
     } 
    } 

    HttpContext.Current.Response.Headers.Add("Content-Type", "application/json"); 

    return data; 
} 

這裏是我的translations.js文件:

define(['jquery', 'underscore'], function ($) { 

    function translations() { 
    } 

    _.extend(translations.prototype, { 
     target: '/api/resources/translations', 

     getTranslations: function() { 
      return $.ajax({ 
       url: 'api/resources/translations', 
       type: 'GET', 
       dataType: 'json' 
      }); 
     } 
    }); 

    return (translations); 
}); 

因爲我的幾個其他文件依賴在翻譯現有的,我需要窩在我main.js 2點RequireJS聲明:

requirejs(['application/translations', 'whatever other resources that can load that don't depend on translations'], function() { 
    var trans = new translations(); 

    trans.getTranslations() 
     .done(function (result) { 
      // set translations into a variable, we chose it to be part of the global namespace 
      window.Translations = result; 

      // load remaining dependencies that require translations to exist 
      requirejs(['myotherjsfiles', 'blahblah', function() { 
       // you get the idea... 
      }); 
     }); 
}); 

這允許我的翻譯首先加載(與任何非依賴文件,如bootstrap,jquery等),然後加載所有我的依賴JavaScript文件。我也根據RequireJs優化方法測試了它,並且它能夠解析嵌套的需求。希望這可以幫助別人解決如何將翻譯下載到客戶端和/或如何使用RequireJS加載動態模塊。

如果有人知道如何讓WebApi返回JavaScript,我很樂意聽到它!

乾杯!

+0

它不會解決您的問題,你有2個選擇:使其餘的代碼異步並等待api調用的結果或使您的AJAX調用同步將bl在等待服務器響應時,執行應用程序。 – mpm

+0

早些時候,我將$ .get更改爲$ .ajax,並將async指定爲false,但是,這並未延遲requirejs加載下一個文件。看起來很奇怪,沒有辦法構建一串JavaScript並能夠以JavaScript的形式接收它。我沒有看到選項1是一個可行的選擇。 –

+0

您可以編寫您嘗試進行同步操作的代碼嗎?以及函數的使用環境?事實是,你的代碼看起來有點奇怪。 – mpm

回答

4

你想這樣的問題: Is there a way to force ASP.NET Web API to return plain text?

你並不需要創建一個PlainTextFormatter(我認爲唯一的好處是,當被要求text/plain自動拍攝?),你只需要一個輔助方法:

/// <summary> 
    /// Generate raw content from the given string output. 
    /// <para>See https://stackoverflow.com/a/13028027/1037948 and https://stackoverflow.com/a/11582207/1037948 </para> 
    /// </summary> 
    /// <param name="responseBody"></param> 
    /// <param name="mediaType"></param> 
    /// <returns></returns> 
    private HttpResponseMessage getRawResponse(string responseBody, string mediaType = "text/plain") { 

     var response = Request.CreateResponse(HttpStatusCode.OK); 
     response.Content = new StringContent(responseBody, Encoding.UTF8, mediaType); 
     return response; 
    } 

那麼對於任何GetSomething方法(S),你寫的:

public object GetMySomething1() { 

     return getRawResponse(string.Format(@"window.SomeValue = {{{0}}}", 
          string.Join(",", 
             Enum.GetValues(typeof(RandomEnum)).Cast<RandomEnum>().Select(o => string.Format("{0}:\"{1}\"", o, o))) 
      )); 
    } 

導致請求/myapicontroller/getmysomething1/(假設你的路徑設置爲允許行動)返回:

window.SomeValue = {RandomValue1:"RandomValue1",RandomValue2:"RandomValue2",...}