Expanding upon the Ember.js Model's save functionality

I have incorporated promise-based code (MyLib) that needs to execute on every model save by extending the save function:

DS.Model.extend
  save: ->
    parentSave = @_super.bind this, arguments...
    deferred   = Ember.RSVP.defer()

    MyLib.func().then((val) =>
      @set 'prop', val

      parentSave()
        .then(deferred.resolve)
        .fail(deferred.reject)
    )

    DS.PromiseObject.create promise: deferred.promise

Please note that the promise returned by MyLib.func() utilizes an rsvp.js promise instead of an Ember.RSVP promise.

Although this solution appears to be effective in practice, certain test cases are failing with the error message

You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in an Ember.run
.

Even after attempting to encapsulate the @set and parentSave calls within Ember.run, I am still encountering asynchronous issues during testing.

Thus, my inquiry is centered around how to extend the save function with asynchronous code while ensuring compatibility with testing procedures?

Answer №1

Two key points:

  1. The error you're experiencing is likely due to the fact that all asynchronous code needs to be run within Ember's run loop. This means that when your promise resolves and executes the deferred.resolve method, it should be done within the run loop. So your code should look something like this:

    parentSave().then(() ->
        Em.run.next(() ->
            deferred.resolve()
        )
    )
    

    I hope this helps clarify things for you. Let me know if you need any further assistance.

  2. Starting with Ember 1.5.0, calling _super in a certain way is no longer supported. It should now be called inline rather than asynchronously.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Eliminating unnecessary whitespace between the bottom of the document and an element

I have a HTML page that when scrolled should limit scroll to the document height. It must not exceed the bottom of an element. Here is a screenshot for better understanding: https://i.stack.imgur.com/RaVg8.jpg The scrolling should be limited after the En ...

How to Extract a Link from JSON Data in React Native

My JSON data is formatted like this: orderData:"<p>Key VVV: 6326233</p> <p>Download link <a title=\"Movie\" href=\"https://play.google.com/store/movies/details/The_Angry_Birds_Movie_2?id=O_RbjOHHpIs&hl=en\" t ...

JavaScript code to change an array into a stringified format

I have encountered a challenge with transforming an array. The original array looks like this: array = [ { "name": "name", "value": "Olá" }, { "name": "age" ...

How can I resolve the "web page not found" error when using jQuery .replace()?

Working on HTML and javascript/jquery scripts, I have encountered a peculiar issue. My script utilizes a for-in loop to iterate through a JavaScript object, substituting specific patterns in HTML-formatted lines with data from the object using jQuery .appe ...

Suggestions for retaining dynamically modified HTML content post-response

My registration form includes input fields for username, email, and more. I have implemented a script that validates the form upon clicking the submit button, turning labels red when fields are empty. However, the submit button is also configured to send a ...

Create a stylish frame for designated rows within a table

Col1 Col2 Col3 Col4 Col5 Col6 Row11 Row12 Row13 Row14 Row15 Row16 Row21 Row22 Row23 Row24 Row25 Row26 Row31 Row32 Row33 Row34 Row35 Row36 Looking for a way to add a border around rows with matching values in the first column, or around a specif ...

How can I use jQuery to fix the positioning of a right-side div?

How can I make the right hand side div fixed within row two, so that it stays in place when scrolling down the page and moves up when reaching the footer area? Here is an example of my code: I would like to keep the col-md-4 class fixed when it reaches t ...

Can Python's datetime object be used interchangeably with a JavaScript date object?

Take for instance in python, a date might look like: 2020-06-19T11:32:16.548109Z, is there a method I can utilize to transform this into a javascript Date object? ...

Firefox causing issues with Rails Ajax functionality

My Rails application includes an AJAX call that currently triggers a JavaScript alert message. While this functionality works smoothly on Safari and Chrome, it strangely fails to function on Firefox (across all its recent versions). I tried debugging the c ...

ng-model establishes a connection with objects, not properties

Having just started my journey with AngularJS and JavaScript, I decided to create a simple app that allows users to input their name and age, and then displays the list of users and their ages. Here is the code I put together: var main = angular.module( ...

"Retrieve objects from an array based on specified minimum and maximum values, while also checking for any null

My current dataset is structured as follows: const data = [ { id: '11se23-213', name: 'Data1', points: [ { x: 5, y: 1.1 }, { x: 6, y: 2.1 }, { x: 7, y: 3.1 }, { x: 8, y: 1.5 }, { x: 9, y: 2.9 ...

Transmitting a multidimensional array in JavaScript to an AjaxPro method on a front-end webpage: A step-by-step guide

Imagine a scenario where I have a C# method that requires an array parameter: [AjaxPro.AjaxMethod] public int Test3(string[,] array) { return array.Length; } Next, I define a multidimensional array on the front page using JavaScript: var array = ...

Ways to invoke a JavaScript function using a JSON string

Suppose I make an AJAX post request using jQuery with the following structure: $.post('MyApp/GetPostResult.json', function(data) { // what should be implemented here? }); When the result is as follows: { "HasCallback": true, "Cal ...

variations that arise when properties are included in an extended class

In my possession is the following code snippet: class sampleError extends Error { constructor(message, errorCode) { super(message); //Include a "message" property this.code = errorCode //Include a "code" property" } } ...

Having trouble disabling JavaScript with Selenium

I'm attempting to disable JavaScript through the Selenium profile when launching a browser. This method has worked in the past, but after updating my Selenium/Firefox versions, I am encountering difficulties. profile = webdriver.FirefoxProfile() ...

Is there a way to detect and handle errors triggered by a callback function?

My component has the following code snippet: this.loginService.login(this.user, () => { this.router.navigateByUrl('/'); }); Additionally, my service contains this method: login(credentials, callback) { co ...

Error thrown by Text.splitText due to AJAX request

I have a javascript webpage where I am attempting to make an Ajax request like this. <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style. ...

Concealing a form after submission using JavaScript

After submitting the form, I am attempting to hide it and display a loading GIF. I've experimented with various methods, including changing ClassName to Id, but haven't had success. This is for a school project, and I've spent a significant ...

Opening a Text File via an ASPX Web Page

Currently, I am immersed in a Web Pages project. The goal is to retrieve text from a Text File and display it within a div element. To achieve this, I utilized the ajax xmlhttprequest method, following a tutorial available at http://www.w3schools.com/ajax/ ...

Tips for verifying if the Selection object includes two adjacent elements that are both either 'Div' or 'P' nodes

I'm struggling to figure out how to determine if a user's selection contains two or more sibling divs or paragraphs. I'm new to working with the DOM and could use some guidance on this issue. Has anyone encountered this situation before, or ...