Using models in a flux application
I have a question about design pattern.
Sometimes store can hold complex data structures that are passed to components as plain javascript objects. However, sometimes there can be some logic related to the data, that certain components may need.
for example let's take a chat application and a store that holds conversation objects:
conversation = {
messages: [{
body: 'hello',
from: 'Alice',
unread: true
}, {
body: 'hi',
from: 'Bob',
unread: 'false'
}]
}
some application components may need to get some derived data that they want to render and this data is usually retrieved using a set of utility functions. examples:
function isConversationUnread(conversation) {
return conversation.messages.some(message => message.unread === true);
}
function getParticipants(conversation) {
return conversation.messages.map(message => message.from);
}
//...
the alternative solution is to define a class (which becomes an MVC style model) that holds all data related functions as methods that any component can use. example
class Conversation {
constructor(conversationData) {
Object.assign(this, conversationData);
}
isConversationUnread() {
// ...
}
getParticipatns() {
// ..
}
}
the question is - what are the benefits or issues of these approaches? Is OOP style considered good or bad?
Using a class for a store, or for part of a store, does not necessarily mean you have fallen into MVC. We haven't shown this in our examples simply because most of the time this isn't needed. Stores are themselves models -- a special kind of model called a "domain model", where they manage state for particular logical domain.
OOP style is fine if you prefer that style. I think Flux works with a relatively OOP style -- it's the data flow that Flux emphasizes, which is more like functional programming. Some people take this further, as in Redux, where stores are even more functional in their structure and usage.