3

i'm making CRUD admin tool using Nuxt.js (Vue.js SSR framework) but when object more nested, it is difficult to handling this objects.

for example, when i have object like this

in A.vue file

data: () => ({
    page:  {
       language: "",
       background: "",
       content: { 
          title: "",
          age: {
             male: ["......."],
             female: ["...."]
          }  
       }
       ...and more complex
    }
})

i want using class model like this,

in A.vue file

data: () => ({
    page: new Page();
})

in Class files...

class Page() {
   language, background, content properties...
}
class Content() {
    title, age properties...
}
class Age() {
    male, female properties...
}

but class is not a pure object, it does not match the concept of vue.js (Vue will walk through all of its properties and convert them to getter/setters using Object.defineProperty.)

so, i used util function that get data, and returning page object with default values,

in A.vue

data: () => ({
    page: getPageObject()
})

in PageUtils.js

function getPageObejct(data) {
    return {
       ....data,
       language: 'en',
       background: '',
       content: {
           title: 'title'
           ....
       }
     }
 }

...but i don't think that best way of handle complex data

how can i using class or other mapping model for convenient using data? (default value, method..)

2
  • 1
    Use state management. You don't have to clatter all of the data in a component, rather make a store for each module in your app. I have a multiple store sample in one of my github project. You check it our here. github.com/jofftiquez/wakeupbilliejoe.com Commented Feb 22, 2018 at 7:05
  • i don't think it about store concept .... Commented Feb 22, 2018 at 8:43

1 Answer 1

1

Vue is not immutable. Vue components want to be stateless chatty interfaces to a store.

The page on state management is here. You can see there's a lot of openess to different approaches. But vue relies on being able to observe changes in store state.

Keeping state in Vue components will make life needlessly harder. As others have noted, Vue's event model is not sophisticated. You're fine as long as your data is used only within your component, but the lesson of history is that just about every item of app state will eventually have more than one representation on screen, and then you're in trouble.

Sign up to request clarification or add additional context in comments.

4 Comments

i'm using nuxt.js, component fetching data and using it directly.. so i want to mapping model for fetched data. it not about store concept. and Immutable.js not compatible with Vue.js...? here is link about that forum.vuejs.org/t/immutable-js-with-vue/6366
it is better using store management like vuex if handle common data for components.. but when fetching data from server and using it directly, store is not necessarily needed.
i'm focus on using fetched data directly in component using mapping model (just like es6 class)...
Yep. I suspect this is a recipe for making life needlessly harder. As others have noted, Vue's event model is not sophisticated. You're fine as long as your data is used only within your component, but the lesson of history is that just about every item of app state will eventually have more than one representation on screen, and then you're in trouble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.