Comments (6)
I think how you could structure your state is like:
{
products: [
{ id: 1, addingProduct: false },
{ id: 3, addingProduct: false }
]
}
This way, you just do products.push(...)
to add new items on the page.
It's not supported to dynamically define properties on the binding.
from simulacra.
A big object that renders everything is exactly how I use it. Here's an example: https://github.com/fortunejs/fortune-http/blob/master/lib/html_serializer.js
I would recommend using it on both server and client if you're going to be re-using existing HTML, the reason for that is it should guarantee that the state and the rendered HTML are consistent.
If I understand correctly, you don't want to fully render all of the HTML on the server-side, and let the browser make AJAX requests to get more info? Why not have the server render it?
I'm working on a large single page app using simulacra.js and I'd love to open source it, but it's just not ready yet.
from simulacra.
Thanks for the quick response
- The server app is written in PHP
- The AJAX requests happen upon user interaction
Assuming the state object is called appState
(which is initially and partially populated by the server) when the user clicks on an "Add to cart" button i want to
- Set
appState.UI.addingProductToCart_{id_of_the_product} = true
- Have simulacra disable the "add to cart" button and display a spinner because it detected the change at 1)
- Make an AJAX call and wait for the response
- On success, have simulacra update a shopping cart widget with the new details of the cart
- Set
appState.UI.addingProductToCart_{id_of_the_product} = false
- Have simulacra re-enable the "add to cart button"
As you can see, I need to be able to attach different properties to the appState
, properties that I don't know. I've seen you are using Object.defineProperty
which requires knowing in advance what properties are set on an object.
from simulacra.
You can define a binding like:
{
addingProduct: [ 'button.add-product', function (node, value) {
node.classList[value ? 'add' : 'remove']('loading-spinner')
} ]
}
Make sure your initial state includes:
{
addingProduct: false
}
How you handle the AJAX request is up to you, but you could do something like:
appState.addingProduct = true
ajax(...).then(function(result) {
appState.addingProduct = false
})
By the way, it might not be a good idea to use IDs as the property, because you'd have to define them up front. Better way might be to have a separate property called id
, and use arrays of objects.
from simulacra.
The thing is there are numerous products on a page and user can click on few 'add to cart' buttons. So my state should have something like
{
addingProduct_1: false,
addingProduct_3: false,
}
So the trouble is that I have to know in advance all the properties of the state. In the case of a static page (ie: one where the product list is fixed for the duration of the page) this is possible (although cumbersome) but if I were to implement infinite scrolling I need to be able to add properties on the fly.
Is it possible to do this outside simulacra? Something like this
function set_on_state(simulacraStateObject, property, value) {
// I don't know what should be here though
simulacraStateObject.defineProperty(property);
simulacraStateObject.property = value;
}
from simulacra.
Thank you. Why didn't I think of this :)
from simulacra.
Related Issues (20)
- Example use cases HOT 1
- Initialize Change Function HOT 5
- Lisp rewrite HOT 2
- How to add change binding HOT 10
- Split helpers into separate build
- Rework path
- Home site simulacra.js.org does not work in IE11 HOT 3
- Data propagation from DOM to state HOT 2
- Question: detect mount of the whole component HOT 17
- TODOs
- [Homepage/Usage example]: Example not working (ReferenceError: x is not defined) HOT 2
- Conditional rendering example HOT 12
- More example
- JS Benchmark - Keyed Version HOT 1
- cdn example HOT 5
- How to bind to dictionary HOT 2
- IE11 table bindObject render error HOT 3
- Why does order of the bindings matter? HOT 3
- Is it safe to move DOM nodes? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simulacra.