Skip to content

Commit 4283880

Browse files
committed
Fix bug that prevented multiple changes from sticking after 2nd save event.
1 parent 6ac1649 commit 4283880

13 files changed

+247
-288
lines changed

bower.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "angular-point-discussion-thread",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"main": "dist/angular-point-discussion-thread.js",
55
"homepage": "https://github.com/scatcher/angular-point-discussion-thread",
66
"authors": [
77
"Scott Hatcher <[email protected]>"
88
],
99
"description": "Simple discussion thread directive for angular-point.",
1010
"dependencies": {
11-
"angular": "^1.4.0",
11+
"angular": "^1.4.1",
1212
"angular-point": "^3.2.0",
13-
"lodash": "^3.7.0"
13+
"lodash": "^3.10.0"
1414
},
1515
"keywords": [
1616
"angular-point"

dist/angular-point-discussion-thread.js

Lines changed: 122 additions & 127 deletions
Large diffs are not rendered by default.

dist/angular-point-discussion-thread.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "angular-point-discussion-thread",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "Simple discussion thread directive for angular-point.",
55
"main": "dist/angular-point-discussion-thread.js",
66
"scripts": {
77
"test": "gulp test"
88
},
99
"devDependencies": {
1010
"gulp": "~3.9.0",
11-
"gulp-angular-templatecache": "^1.6.0",
12-
"gulp-concat": "^2.5.2",
11+
"gulp-angular-templatecache": "^1.7.0",
12+
"gulp-concat": "^2.6.0",
1313
"gulp-sourcemaps": "^1.5.2",
14-
"gulp-typescript": "^2.7.6",
15-
"lodash": "~3.9.3",
14+
"gulp-typescript": "^2.7.8",
15+
"lodash": "~3.10.0",
1616
"typescript": "^1.5.0-beta"
1717
},
1818
"engines": {

src/DiscussionThread.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ module ap.discussionThread {
77
export interface IDiscussionThread {
88
createPost(parentId: number, content: string): Post;
99
getDiscussionAttributeName(): string;
10-
getListItem(): ap.IListItem<any>;
10+
getListItem(): ap.ListItem<any>;
1111
getNextId(): number;
1212
posts: Post[];
1313
prune(): void;
1414
reset(): void;
15-
saveChanges(): ng.IPromise<ap.IListItem<any>>;
15+
saveChanges(): ng.IPromise<ap.ListItem<any>>;
1616
}
1717

1818
/**
@@ -23,10 +23,10 @@ module ap.discussionThread {
2323
* @returns {object} Newly instantiated discussion object.
2424
*/
2525
export class DiscussionThread implements IDiscussionThread {
26+
getDiscussionAttributeName: () => string;
27+
getListItem: () => ap.ListItem<any>;
2628
posts: Post[] = [];
27-
getListItem;
28-
getDiscussionAttributeName;
29-
constructor(listItem: ap.IListItem<any>, discussionAttributeName = 'discussionThread') {
29+
constructor(listItem: ap.ListItem<any>, discussionAttributeName = 'discussionThread') {
3030

3131
/** Protect these two paramaters to prevent saving to SharePoint */
3232
this.getListItem = () => listItem;
@@ -35,7 +35,7 @@ module ap.discussionThread {
3535
_.assign(this, listItem[discussionAttributeName]);
3636

3737
_.each(this.posts, (post, index) => {
38-
this.posts[index] = new Post(post, this);
38+
this.posts[index] = new Post(post);
3939
});
4040
}
4141
createPost(parentId: number, content: string): Post {
@@ -45,7 +45,7 @@ module ap.discussionThread {
4545
parentId: parentId || 0,
4646
created: new Date(),
4747
user: user
48-
}, this);
48+
});
4949

5050
this.posts.push(newPost);
5151
return newPost;
@@ -69,7 +69,7 @@ module ap.discussionThread {
6969
reset(): void {
7070
this.posts.length = 0;
7171
}
72-
saveChanges(): ng.IPromise<ap.IListItem<any>> {
72+
saveChanges(): ng.IPromise<ap.ListItem<any>> {
7373
this.prune();
7474
var listItem = this.getListItem();
7575
return listItem.saveFields([this.getDiscussionAttributeName()]);

src/Post.ts

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,38 @@
44
module ap.discussionThread {
55
'use strict';
66

7-
export interface IPost {
7+
export class Post {
88
created: Date;
99
content: string;
10-
deletePost: Function;
1110
id: number;
12-
getThread(): DiscussionThread;
1311
parentId: number;
14-
removePost: Function;
15-
reply: Function;
16-
savePost: Function;
1712
user: ap.IUser;
18-
}
19-
20-
export class Post implements IPost {
21-
created: Date;
22-
content: string;
23-
id: number;
24-
parentId: number;
25-
user: ap.IUser;
26-
getThread;
27-
28-
constructor(post, thread) {
2913

30-
this.getThread = () => thread;
14+
constructor(post) {
3115
_.assign(this, post);
3216
if (_.isString(this.created)) {
3317
/** Convert stringified date back into JS date */
3418
this.created = moment(this.created).toDate();
3519
}
3620
}
3721

38-
deletePost() {
39-
this.removePost();
40-
return this.savePost();
22+
deletePost(discussionThread: DiscussionThread) {
23+
this.removePost(discussionThread);
24+
return this.savePost(discussionThread);
4125
}
4226

43-
removePost(): void {
44-
var thread = this.getThread();
45-
var index = thread.posts.indexOf(this);
46-
thread.posts.splice(index, 1);
27+
removePost(discussionThread: DiscussionThread): void {
28+
var index = discussionThread.posts.indexOf(this);
29+
discussionThread.posts.splice(index, 1);
4730
}
4831

49-
reply(response: string) {
50-
var thread = this.getThread();
51-
thread.createPost(this.id, response);
52-
return this.savePost();
32+
reply(response: string, discussionThread: DiscussionThread) {
33+
discussionThread.createPost(this.id, response);
34+
return this.savePost(discussionThread);
5335
}
5436

55-
savePost() {
56-
var thread = this.getThread();
57-
return thread.saveChanges();
37+
savePost(discussionThread: DiscussionThread) {
38+
return discussionThread.saveChanges();
5839
}
5940
}
6041
}

src/ap-discussion-thread-post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div ng-include="'ap-discussion-thread-new-post.html'" ng-init="tempState = 'tempResponse'"></div>
1616
</div>
1717
<ul class="child-thread">
18-
<li ng-repeat="response in vm.discussionObject.posts | filter:{parentId: post.id} track by response.id">
18+
<li ng-repeat="response in vm.discussionThread.posts | filter:{parentId: post.id} track by response.id">
1919
<div ng-include="'ap-discussion-thread-post.html'" ng-init="post = response; level = level + 1"></div>
2020
</li>
2121
</ul>

src/ap-discussion-thread.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
</legend>
1313
<div ng-include="'ap-discussion-thread-new-post.html'" ng-init="tempState = 'tempPost'"></div>
1414
</fieldset>
15-
<fieldset ng-if="vm.discussionObject.posts.length > 0">
15+
<fieldset ng-if="vm.discussionThread.posts.length > 0">
1616
<legend>
1717
<small>Discussion Thread</small>
1818
</legend>
1919
<ul class="ap-discussion-thread well well-sm">
20-
<li class="discussion-head" ng-repeat="post in vm.discussionObject.posts | filter:{parentId: 0} track by post.id" style="border-top-width: 1px;border-top-color: grey">
20+
<li class="discussion-head" ng-repeat="post in vm.discussionThread.posts | filter:{parentId: 0} track by post.id"
21+
style="border-top-width: 1px;border-top-color: grey">
2122
<div ng-include="'ap-discussion-thread-post.html'" ng-init="level = 0"></div>
2223
</li>
2324
</ul>

src/apDiscussionThreadDirective.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
module ap.discussionThread {
55
'use strict';
66

7-
8-
97
export var APDiscussionThreadDirective = () => {
108
var directive = {
119
controller: DiscussionThreadController,

src/apDiscussionThreadDirectiveController.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ module ap.discussionThread {
77
interface IControllerScope extends ng.IScope {
88
changeEvent?(action: string, content?: string): void;
99
fieldName?: string;
10-
listItem: ap.IListItem<any>;
10+
listItem: ap.ListItem<any>;
1111
}
1212

1313
export class DiscussionThreadController {
14-
changeEvent(action: string, content?: string);
15-
16-
discussionObject: DiscussionThread;
14+
changeEvent: (action: string, content?: string) => void;
15+
discussionThread: DiscussionThread;
1716
fieldName: string;
1817
listItem: Object;
1918
negotiatingWithServer: boolean = false;
20-
posts: Post[];
2119
respondingTo = '';
2220
tempPost = '';
2321
tempResponse = '';
@@ -28,11 +26,12 @@ module ap.discussionThread {
2826
vm.fieldName = $scope.fieldName || 'discussionThread';
2927
vm.listItem = $scope.listItem;
3028

31-
$scope.$watch('listItem', (newVal, oldVal) => {
29+
$scope.$watch('listItem.' + vm.fieldName, (newVal, oldVal) => {
3230
if (newVal) {
33-
vm.discussionObject = vm.listItem[vm.fieldName];
34-
vm.posts = this.discussionObject.posts;
31+
vm.discussionThread = newVal;
3532
}
33+
34+
console.assert(newVal.posts === vm.listItem[vm.fieldName].posts);
3635
});
3736

3837
}
@@ -58,19 +57,20 @@ module ap.discussionThread {
5857
} else {
5958
this.negotiatingWithServer = true;
6059
if (post) {
61-
post.reply(content)
60+
post.reply(content, this.discussionThread)
6261
.then(() => this.cleanup('reply', content));
6362
} else {
6463
/** Creating new top level post */
65-
this.discussionObject.createPost(null, content).savePost()
64+
this.discussionThread.createPost(null, content)
65+
.savePost(this.discussionThread)
6666
.then(() => this.cleanup('create', content));
6767
}
6868
}
6969
}
7070

7171
deletePost(post: Post) {
7272
var hasChildren = false;
73-
_.each(this.discussionObject.posts, (p) => {
73+
_.each(this.discussionThread.posts, (p) => {
7474
if (p.parentId === post.id) {
7575
hasChildren = true;
7676
return;
@@ -84,7 +84,7 @@ module ap.discussionThread {
8484
var confirmation = window.confirm('Are you sure you want to delete this comment?');
8585
if (confirmation) {
8686
this.negotiatingWithServer = true;
87-
post.deletePost()
87+
post.deletePost(this.discussionThread)
8888
.then(() => this.cleanup('delete'));
8989
}
9090
}

src/apDiscussionThreadFactory.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ module ap.discussionThread {
2929
* @param {string} discussionAttributeName Name of attribute on list item.
3030
* @returns {object} Newly instantiated discussion object.
3131
*/
32-
createDiscussionObject<T>(listItem: ap.IListItem<T>, discussionAttributeName = 'discussionThread'): DiscussionThread {
32+
createDiscussionObject<T>(listItem: ap.ListItem<T>, discussionAttributeName = 'discussionThread'): DiscussionThread {
3333
var discussionThread;
3434

35-
if (listItem[discussionAttributeName] && listItem[discussionAttributeName].constructor.name === 'DiscussionThread') {
36-
/** Discussion thread is already instantiated */
37-
discussionThread = listItem[discussionAttributeName];
38-
} else {
35+
// if (listItem[discussionAttributeName] && listItem[discussionAttributeName].constructor.name === 'DiscussionThread') {
36+
// /** Discussion thread is already instantiated */
37+
// discussionThread = listItem[discussionAttributeName];
38+
// } else {
3939
/** Instantiate and return new discussion thread */
4040
discussionThread = new DiscussionThread(listItem, discussionAttributeName);
41-
}
41+
// }
4242

4343
return discussionThread;
4444
}

typings/angularjs/angular.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ declare module angular {
10641064
///////////////////////////////////////////////////////////////////////////
10651065
interface IAnchorScrollService {
10661066
(): void;
1067+
(hash: string): void;
10671068
yOffset: any;
10681069
}
10691070

@@ -1414,6 +1415,7 @@ declare module angular {
14141415
* https://docs.angularjs.org/api/ng/service/$http#defaults
14151416
*/
14161417
interface IHttpProviderDefaults {
1418+
cache?: boolean;
14171419
xsrfCookieName?: string;
14181420
xsrfHeaderName?: string;
14191421
withCredentials?: boolean;

0 commit comments

Comments
 (0)