Skip to content

Instantly share code, notes, and snippets.

@freewind
Created January 31, 2013 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freewind/4683847 to your computer and use it in GitHub Desktop.
Save freewind/4683847 to your computer and use it in GitHub Desktop.
A controller in my angularjs application
admin.controller 'modules.wind_articles.categories.index.Ctrl', ["$scope", "JsRoutes", "ExData", "Commons", ($scope, jsRoutes, exData, commons) ->
$scope.categoryTree = exData.categoryTree
$scope.createCategory = ->
jsRoutes.wind_articles.Categories.create.post
parentId: (if not $scope.selectedNode? then null else $scope.selectedNode.code)
name: $scope.addName
, (node) ->
if $scope.selectedNode?
$scope.selectedNode.children.push node
else
$scope.categoryTree.push node
$scope.addCategoryModalShown = false
$scope.selectedNode = null
$scope.addName = null
$scope.showAddCategoryModal = (parent) ->
$scope.selectedNode = parent
$scope.addCategoryModalShown = true
$scope.remove = (node) =>
if confirm("确定删除吗?")
jsRoutes.wind_articles.Categories.remove.post
id: node.code
, ->
parent = @getParentNode($scope.categoryTree, node)
unless parent?
$scope.categoryTree = _.without($scope.categoryTree, node)
else
parent.children = _.without parent.children, node
$scope.updateTree = ->
jsRoutes.wind_articles.Categories.updateOrder.post $scope.categoryTree, (->
), null,
postType: "json"
$scope.showParentModal = (node) ->
$scope.selectedNode = node
$scope.changeParentModalShown = true
$scope.changeParent = (parentNode) =>
jsRoutes.wind_articles.Categories.changeParent.post
id: $scope.selectedNode.code
parentId: (if not parentNode? then null else parentNode.code)
, =>
$scope.changeParentModalShown = false
node = $scope.selectedNode
$scope.selectedNode = null
oriParent = @getParentNode($scope.categoryTree, node)
if oriParent?
oriParent.children = _.without(oriParent.children, node)
else
$scope.categoryTree = _.without($scope.categoryTree, node)
unless parentNode?
$scope.categoryTree.push node
else
parentNode.children.push node
$scope.getArticles = (node) ->
$scope.selectedNode = node
jsRoutes.wind_articles.Articles.list.get
categoryId: (if not node? then null else node.code)
, (data) ->
$scope.articles = data
$scope.showAddArticleModal = =>
if $scope.addArticleModalShown is true
$scope.addArticleModalShown = false
else
@resetArticleModal $scope
$scope.articleCategoryNode = $scope.selectedNode
$scope.addArticleModalShown = true
$scope.createArticle = ->
jsRoutes.wind_articles.Articles.create.post
categoryId: $scope.selectedNode.code
title: $scope.articleAddTitle
content: $scope.articleAddContent
, (article) ->
$scope.articles[0...0] = [ article ]
$scope.addArticleModalShown = false;
$scope.removeArticles = ->
if confirm("确定删除吗?")
ids = $scope.getAllCheckedIds()
jsRoutes.wind_articles.Articles.remove.post
ids: ids
, ->
$scope.articles = _.reject $scope.articles, (item) -> _.contains ids, item.id
$scope.editArticle = ->
if $scope.editArticleModalShown
$scope.editArticleModalShown = false
else
id = $scope.getUniqueCheckedId()
article = _.find $scope.articles, (article) -> article.id == id
$scope.editArticleId = id
$scope.editArticleTitle = article.title;
$scope.editArticleContent = article.content
$scope.editArticleCategoryId = article.categoryId
$scope.editArticleModalShown = true
$scope.updateArticle = ->
id = $scope.getUniqueCheckedId()
jsRoutes.wind_articles.Articles.update.post
id: $scope.editArticleId
title: $scope.editArticleTitle
content: $scope.editArticleContent
, ->
article = _.find $scope.articles, (article) -> article.id == id
article.title = $scope.editArticleTitle
article.content = $scope.editArticleContent
$scope.editArticleModalShown = false
$scope.getRows = ->
$scope.articles
commons.checkboxConnect $scope
$scope.getArticles null
@getParentNode = (nodes, targetNode) ->
visitNodes = (nodes) ->
_.each nodes, (node) ->
throw node if _.contains(node.children, targetNode)
visitNodes node.children
return null if _.contains(nodes, targetNode)
try
visitNodes nodes
return null
catch node
return node
@resetArticleModal = (scope) ->
scope.articleAddContent = ""
scope.articleAddTitle = ""
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment