Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:33:41+00:00 2026-05-31T21:33:41+00:00

I am trying to implement Repository pattern in JavaScript. I have ViewModel which i

  • 0

I am trying to implement Repository pattern in JavaScript. I have ViewModel which i want to initialize with the data when i call Initialize method on it. Everything seems to be falling in places except that i am not able to return the data from my AJAX call. I can see that data is coming back from the ajax call but when i trying to capture the data in SomeViewModel’s done function, it is null.

Can someone please point me out where i am going wrong here?

P.S: Please notice that i am not making Async call so the call chain is properly maintained.

This is how my Repository looks like:

function SomeRepository(){
    this.LoadSomeData = function loadData()
    {
        $.ajax({
            type: "POST",
            url: "someUrl",
            cache: true,
            async: false,
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            //success: handleHtml,
            success: function(data) {
                alert('data received');
                return data; 
            },

            error: ajaxFailed
        });



        function ajaxFailed(xmlRequest) {
                alert(xmlRequest.status + ' \n\r ' + 
                xmlRequest.statusText + '\n\r' + 
                xmlRequest.responseText);
        }

    }




};

This is how my ViewModel looks like:

function SomeViewModel(repository){   
    var self = this;
    var def = $.Deferred();

    this.initialize = function () {
    var def = $.Deferred();
    $.when(repository.LoadSomeData())
    .done(function (data) {
        def.resolve();
    });
    return def;
  };

}

This is how i am calling from an aspx page:

var viewModel = new SomeViewModel(new SomeRepository());
viewModel.initialize().done(alert('viewmodel initialized'));
alert(viewModel.someProperty);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:33:42+00:00Added an answer on May 31, 2026 at 9:33 pm

    I have used successfully an auxiliar variable to put the ajax result, when ajax call is inside a function (only works if ajax is async=false) and i need the function does return the ajax result. I don’t know if this is the best solution.

    function ajaxFunction(){
        var result='';
        $.ajax({
            type: "POST",
            url: "someUrl",
            cache: true,
            async: false,
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            //success: handleHtml,
            success: function(data) {
                alert('data received');
                result=data; 
            },
    
            error: ajaxFailed
        });
        return result;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the repository pattern using a generic repository like the one
I am trying to implement a handy data repository or knowledge base for a
In trying to implement the repository pattern I've run into a minor problem that
I'm trying to implement the repository pattern with ef4 ctp5, I came up with
I'm trying to teach myself the repository pattern, and I have a best practices
I'm trying to implement the repository pattern using entity framework code first rc 1.
I am trying to implement a decorator chain for my data-access based on IRepository.
I'm trying to get started with the repository pattern and ASP.NET MVC, and I
All I am trying to find out the correct definition of the repository pattern.
I'm trying to use RIA Services with Repository pattern, Every CRUD operations worked perfectly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.