model_updates 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +30 -0
- data/app/assets/javascripts/model_updates/activate_elements.js +22 -16
- data/lib/model_updates/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2ffdae24565d3467a89c6be21d9c64ac0735957
|
4
|
+
data.tar.gz: 28861eb35b9775b6792ceb67f5321bca1abddd66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c20c840d43c7c0fcc4758e1d85197dd2f891db26f8bc5387dad0c750713e07a10f27255b864f4344c57a702f4f6c430fa5174d02c1df2c24abedff0bc3e71420
|
7
|
+
data.tar.gz: 22183590cfd0b5443e715d1d7e7f6ded411e2f7ec05df1f2b4f8bd41ad230762794c7f92b2e5898489e08ca8a4ef9ecd6eb3d4edd4eec07e98ddfa92b84b5528
|
data/README.md
CHANGED
@@ -62,6 +62,36 @@ Or like this in ERB:
|
|
62
62
|
|
63
63
|
Now that element should update automatically when the model is changed
|
64
64
|
|
65
|
+
## Callbacks
|
66
|
+
|
67
|
+
You can also do a callback, once the value is changed.
|
68
|
+
|
69
|
+
```erb
|
70
|
+
<div class="model-updates" data-model-updates-model="Model" data-model-updates-id="1" data-model-updates-key="updated_at" data-model-updates-callback="myCallback">
|
71
|
+
<%= model.updated_at %>
|
72
|
+
</div>
|
73
|
+
```
|
74
|
+
|
75
|
+
```js
|
76
|
+
function myCallback(data) {
|
77
|
+
if (data.value == "something") {
|
78
|
+
data.element.text("Test: " + data.value)
|
79
|
+
} else {
|
80
|
+
data.element.text(data.value)
|
81
|
+
}
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
The data element is formatted like this:
|
86
|
+
```
|
87
|
+
data = {
|
88
|
+
changes: "A hash of all the registered changes (multiple attributes might by updated than just the one subscribed to in the same update call)",
|
89
|
+
element: "Your original element with the class 'model-updates'",
|
90
|
+
id: "The ID of the model",
|
91
|
+
key: "The key (attribute name) which was updated",
|
92
|
+
value: "The new value of the attribute"
|
93
|
+
}
|
94
|
+
```
|
65
95
|
|
66
96
|
## Contributing
|
67
97
|
|
@@ -24,22 +24,28 @@ $(document).ready(function() {
|
|
24
24
|
console.log("Update from: " + json.model + "(" + json.id + ")")
|
25
25
|
|
26
26
|
for(key in json.changes) {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
27
|
+
elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-key='" + key + "']")
|
28
|
+
elements.each(function() {
|
29
|
+
element = $(this)
|
30
|
+
|
31
|
+
if (element.data("model-updates-callback")) {
|
32
|
+
function_to_call = element.data("model-updates-callback")
|
33
|
+
|
34
|
+
window[function_to_call]({
|
35
|
+
changes: json.changes,
|
36
|
+
element: element,
|
37
|
+
id: json.id,
|
38
|
+
key: key,
|
39
|
+
model: json.model,
|
40
|
+
value: json.changes[key]
|
41
|
+
})
|
42
|
+
} else if(json.changes[key]) {
|
43
|
+
element.text(json.changes[key])
|
44
|
+
} else {
|
45
|
+
// Needs to check if it has a value, else it will print out "null" instead of nothing.
|
46
|
+
element.text("")
|
47
|
+
}
|
48
|
+
})
|
43
49
|
}
|
44
50
|
}
|
45
51
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_updates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|