Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular.js service state reactivity in Vue 3 component computed property #25

Closed
aadv-zygimantas opened this issue Jul 7, 2022 · 2 comments

Comments

@aadv-zygimantas
Copy link

aadv-zygimantas commented Jul 7, 2022

Any tips how I can make angular.js services state reactive with Vue 3 computed property?

My service Slip:


import {
   ...
}

define(['angular'], function () {

  function SlipModel() {
      const Slip = {
          addedEvents: {},
          sliplineAdded(bet) {
                if (!Slip.addedEvents[bet.eventUUID][`${bet.market}|${bet.selection}`]) {   
                    Object.assign(Slip.addedEvents[bet.eventUUID], { [`${bet.market}|${bet.selection}`]: true })
                }
            }
      }
      
      return Slip;
  }

  return SlipModel;

})

This is how I register and export angular.js service:

import ng from 'angular'
import SlipModel from './slipModel'

export let Slip;

ng.module('app.slip', [])
    .factory('Slip', SlipModel)
    .run(/* @ngInject */ function($injector) {
	Slip = $injector.get('Slip')
});

In the Vue 3 component I import it:

import { Slip } from '../../slip/module';
Use it in computed value addedSelection:
computed: {
    addedSelection() {
      return Slip.addedEvents[this.kenoSchedule[0].eventUUID]
    },
  }

The main issue that I'm having is that it's not reactive in Vue 3 computed property, but in Vue 3 HTML template it actually is reactive - if service state changes, I can see those changes in HTML.

I tried to use reactive, toRef, register Slip service as plugin, but the computed value addedSelection just wont update after Slip.addedEvents has a new element pushed/removed.

@aadv-zygimantas
Copy link
Author

I managed to solve my issue by wrapping angular.js service object with reactive:

import {
   ...
}

define(['angular'], function () {

  function SlipModel() {
      const Slip = reactive({
          addedEvents: {},
          sliplineAdded(bet) {
                if (!Slip.addedEvents[bet.eventUUID][`${bet.market}|${bet.selection}`]) {   
                    Object.assign(Slip.addedEvents[bet.eventUUID], { [`${bet.market}|${bet.selection}`]: true })
                }
            }
      })
      
      return Slip;
  }

  return SlipModel;

})

@jaredmcateer
Copy link
Owner

Hello sorry I have been a little preoccupied, I'm glad you found a solution. As you can see I also raised this in #24 and came up with the same conclusion. I'm not entirely sure this is an ideal fix but I haven't thought of anything better yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants